Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
remote ftw
Browse files Browse the repository at this point in the history
  • Loading branch information
bketelsen committed Apr 7, 2021
1 parent f4e7c2e commit b0d4f65
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions blox.cue
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
data_dir: "tests/data"
schema_dir: "tests/schemas"
}
46 changes: 46 additions & 0 deletions cmd/remote.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
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")
}
72 changes: 72 additions & 0 deletions cmd/remote_get.go
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")
}
58 changes: 58 additions & 0 deletions cmd/remote_list.go
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")
}
33 changes: 33 additions & 0 deletions tests/schemas/profile_v1.cue
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
}
}

0 comments on commit b0d4f65

Please sign in to comment.