This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from cueblox/feat/remote-schemas
remote ftw
- Loading branch information
Showing
6 changed files
with
196 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
data_dir: "tests/data" | ||
schema_dir: "tests/schemas" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <repository> <schema name> <version>", | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
data_dir: string | ||
schema_dir: string | *"schemas" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} |