diff --git a/blox.cue b/blox.cue index 013de21..bbd007d 100644 --- a/blox.cue +++ b/blox.cue @@ -1,3 +1,4 @@ { data_dir: "tests/data" + schema_dir: "tests/schemas" } \ No newline at end of file diff --git a/cmd/remote.go b/cmd/remote.go new file mode 100644 index 0000000..da2185f --- /dev/null +++ b/cmd/remote.go @@ -0,0 +1,46 @@ +/* +Copyright © 2021 NAME HERE + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// remoteCmd represents the remote command +var remoteCmd = &cobra.Command{ + Use: "remote", + Short: "Manage remote schemas", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, +} + +func init() { + rootCmd.AddCommand(remoteCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // remoteCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // remoteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/remote_get.go b/cmd/remote_get.go new file mode 100644 index 0000000..bfe7dac --- /dev/null +++ b/cmd/remote_get.go @@ -0,0 +1,72 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "net/http" + "os" + "path" + + "github.com/cueblox/blox/cuedb" + "github.com/cueblox/blox/schema" + "github.com/spf13/cobra" +) + +// getCmd represents the get command +var remoteGetCmd = &cobra.Command{ + Use: "get ", + Short: "Add a remote schema to your repository", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Args: cobra.ExactArgs(3), + + Run: func(cmd *cobra.Command, args []string) { + manifest := fmt.Sprintf("https://%s/manifest.json", args[0]) + res, err := http.Get(manifest) + cobra.CheckErr(err) + var repos schema.Repository + json.NewDecoder(res.Body).Decode(&repos) + schemaName := args[1] + version := args[2] + var selectedVersion *schema.Version + for _, s := range repos.Schemas { + if s.Name == schemaName { + for _, v := range s.Versions { + if v.Name == version { + selectedVersion = v + fmt.Println(v.Name, v.Schema, v.Definition) + } + } + } + } + database, err := cuedb.NewDatabase() + cobra.CheckErr(err) + schemaDir, err := database.GetConfigString("schema_dir") + cobra.CheckErr(err) + err = os.MkdirAll(schemaDir, 0755) + cobra.CheckErr(err) + filename := fmt.Sprintf("%s_%s.cue", schemaName, version) + filePath := path.Join(schemaDir, filename) + err = os.WriteFile(filePath, []byte(selectedVersion.Definition), 0755) + cobra.CheckErr(err) + + }, +} + +func init() { + remoteCmd.AddCommand(remoteGetCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // getCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/remote_list.go b/cmd/remote_list.go new file mode 100644 index 0000000..2eabcb2 --- /dev/null +++ b/cmd/remote_list.go @@ -0,0 +1,58 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/cueblox/blox/schema" + "github.com/pterm/pterm" + "github.com/spf13/cobra" +) + +// listCmd represents the list command +var remoteListCmd = &cobra.Command{ + Use: "list", + Short: "A brief description of your command", + Long: `A longer description that spans multiple lines and likely contains examples +and usage of using your command. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + Args: cobra.ExactArgs(1), + + Run: func(cmd *cobra.Command, args []string) { + manifest := fmt.Sprintf("https://%s/manifest.json", args[0]) + res, err := http.Get(manifest) + cobra.CheckErr(err) + var repos schema.Repository + json.NewDecoder(res.Body).Decode(&repos) + + // TODO extract and reuse with schema_list.go + var td pterm.TableData + header := []string{"Namespace", "Schema", "Version"} + td = append(td, header) + for _, s := range repos.Schemas { + for _, v := range s.Versions { + line := []string{repos.Namespace, s.Name, v.Name} + td = append(td, line) + } + } + pterm.DefaultTable.WithHasHeader().WithData(td).Render() + }, +} + +func init() { + remoteCmd.AddCommand(remoteListCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // listCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/tests/schemas/profile_v1.cue b/tests/schemas/profile_v1.cue new file mode 100755 index 0000000..0ba98e2 --- /dev/null +++ b/tests/schemas/profile_v1.cue @@ -0,0 +1,33 @@ +{ + // No "version", we expect people to use the path + // of the schema to version + _schema: { + name: "blox" + namespace: "schemas.devrel-blox.com" + } + + #Profile: { + _model: { + // Lets assume lowercase Profile is ID + // Lets assume lowercase Profile with _id is the foreign key + // Plural for directory name + plural: "profiles" + } + + name: #Name + address: #Address + } + + #Name: { + forename: string + surname: string + } + + #Address: { + number: string + street: string + city: string + country: string + postcode: string + } +}