Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create CLI using GRPC APIs #71

Merged
merged 18 commits into from
Nov 25, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor: remove init function from cmd files
pyadav committed Nov 11, 2021
commit 8a1c0851b7f7aca6cd84fccb82e9a8d54efd6a7a
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ vendor/
.env
config.yaml
.air.toml
.siren.yaml
bin/air
tmp
dist/
68 changes: 68 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package cmd

import (
"fmt"
"io/ioutil"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

var (
DefaultHost = "localhost"
FileName = ".siren"
FileExtension = "yaml"
)

type configuration struct {
Host string `yaml:"host"`
}

func configCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "manage siren CLI configuration",
}
cmd.AddCommand(configInitCommand())
return cmd
}

func configInitCommand() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "initialize CLI configuration",
RunE: func(cmd *cobra.Command, args []string) error {
config := configuration{
Host: DefaultHost,
}

b, err := yaml.Marshal(config)
if err != nil {
return err
}

filepath := fmt.Sprintf("%v.%v", FileName, FileExtension)
if err := ioutil.WriteFile(filepath, b, 0655); err != nil {
return err
}
fmt.Printf("config created: %v", filepath)

return nil
},
}
}

func readConfig() (*configuration, error) {
var c configuration
filepath := fmt.Sprintf("%v.%v", FileName, FileExtension)
b, err := ioutil.ReadFile(filepath)
if err != nil {
return nil, err
}

if err := yaml.Unmarshal(b, &c); err != nil {
return nil, err
}

return &c, nil
}
18 changes: 8 additions & 10 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -6,15 +6,13 @@ import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(&cobra.Command{
func migrateCommand() *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "Run DB migrations",
RunE: migrate,
})
}

func migrate(cmd *cobra.Command, args []string) error {
c := config.LoadConfig()
return app.RunMigrations(c)
Short: "Migrate database schema",
RunE: func(cmd *cobra.Command, args []string) error {
c := config.LoadConfig()
return app.RunMigrations(c)
},
}
}
13 changes: 9 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -7,12 +7,17 @@ import (
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "siren",
}

// Execute runs the command line interface
func Execute() {
rootCmd := &cobra.Command{
Use: "siren",
}

rootCmd.AddCommand(configCommand())
rootCmd.AddCommand(serveCommand())
rootCmd.AddCommand(migrateCommand())
rootCmd.AddCommand(uploadCommand())

if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
16 changes: 7 additions & 9 deletions cmd/serve.go
Original file line number Diff line number Diff line change
@@ -6,15 +6,13 @@ import (
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(&cobra.Command{
func serveCommand() *cobra.Command {
return &cobra.Command{
Use: "serve",
Short: "Run server",
RunE: serve,
})
}

func serve(cmd *cobra.Command, args []string) error {
c := config.LoadConfig()
return app.RunServer(c)
RunE: func(cmd *cobra.Command, args []string) error {
c := config.LoadConfig()
return app.RunServer(c)
},
}
}
7 changes: 4 additions & 3 deletions cmd/upload.go
Original file line number Diff line number Diff line change
@@ -3,18 +3,19 @@ package cmd
import (
"errors"
"fmt"

"github.com/odpf/siren/client"
"github.com/odpf/siren/config"
"github.com/odpf/siren/pkg/uploader"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(&cobra.Command{
func uploadCommand() *cobra.Command {
return &cobra.Command{
Use: "upload",
Short: "Upload Rules or Templates YAML file",
RunE: upload,
})
}
}

func upload(cmd *cobra.Command, args []string) error {