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!: improve client config cli and rearrange cmd #123

Merged
merged 2 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 17 additions & 5 deletions cli/alert.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package cli

import (
"context"
"fmt"
"os"
"strconv"

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/cmdx"
"github.com/odpf/salt/printer"
"github.com/odpf/siren/pkg/errors"
sirenv1beta1 "github.com/odpf/siren/proto/odpf/siren/v1beta1"
"github.com/spf13/cobra"
)

func alertsCmd(c *configuration) *cobra.Command {
func alertsCmd(cmdxConfig *cmdx.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "alert",
Aliases: []string{"alerts"},
Expand All @@ -29,15 +29,16 @@ func alertsCmd(c *configuration) *cobra.Command {
`),
Annotations: map[string]string{
"group:core": "true",
"client": "true",
},
}

cmd.AddCommand(listAlertsCmd(c))
cmd.AddCommand(listAlertsCmd(cmdxConfig))

return cmd
}

func listAlertsCmd(c *configuration) *cobra.Command {
func listAlertsCmd(cmdxConfig *cmdx.Config) *cobra.Command {
var providerName string
var providerId uint64
var resouceName string
Expand All @@ -53,7 +54,16 @@ func listAlertsCmd(c *configuration) *cobra.Command {
"group:core": "true",
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
spinner := printer.Spin("")
defer spinner.Stop()

ctx := cmd.Context()

c, err := loadClientConfig(cmd, cmdxConfig)
if err != nil {
return err
}

client, cancel, err := createClient(ctx, c.Host)
if err != nil {
return err
Expand All @@ -71,6 +81,7 @@ func listAlertsCmd(c *configuration) *cobra.Command {
return err
}

spinner.Stop()
if res.GetAlerts() == nil {
return errors.New("no response from server")
}
Expand Down Expand Up @@ -106,5 +117,6 @@ func listAlertsCmd(c *configuration) *cobra.Command {
cmd.Flags().StringVar(&resouceName, "resource-name", "", "resource name")
cmd.Flags().Uint64Var(&startTime, "start-time", 0, "start time")
cmd.Flags().Uint64Var(&endTime, "end-time", 0, "end time")

return cmd
}
23 changes: 23 additions & 0 deletions cli/grpc.go → cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@ import (
"context"
"time"

"github.com/odpf/salt/cmdx"
"github.com/odpf/salt/config"
"github.com/odpf/siren/pkg/errors"
sirenv1beta1 "github.com/odpf/siren/proto/odpf/siren/v1beta1"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type ClientConfig struct {
Host string `yaml:"host" cmdx:"host"`
}

func loadClientConfig(cmd *cobra.Command, cmdxConfig *cmdx.Config) (*ClientConfig, error) {
var clientConfig ClientConfig

if err := cmdxConfig.Load(
&clientConfig,
cmdx.WithFlags(cmd.Flags()),
); err != nil {
if !errors.Is(err, new(config.ConfigFileNotFoundError)) {
return nil, err
}
}

return &clientConfig, nil
}

func createConnection(ctx context.Context, host string) (*grpc.ClientConn, error) {
opts := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
Expand Down
83 changes: 42 additions & 41 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,68 @@ package cli

import (
"fmt"
"os"

"github.com/MakeNowJust/heredoc"
"github.com/odpf/salt/cmdx"
"github.com/odpf/salt/printer"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

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

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

func configCmd() *cobra.Command {
func configCmd(cmdxConfig *cmdx.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "manage siren CLI configuration",
Use: "config <command>",
Short: "Manage siren CLI configuration",
Example: heredoc.Doc(`
$ siren config init
$ siren config list
`),
}
cmd.AddCommand(configInitCommand())
cmd.AddCommand(configInitCommand(cmdxConfig))
cmd.AddCommand(configListCommand(cmdxConfig))
return cmd
}

func configInitCommand() *cobra.Command {
func configInitCommand(cmdxConfig *cmdx.Config) *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "initialize CLI configuration",
Short: "Initialize CLI configuration",
Example: heredoc.Doc(`
$ siren config init
`),
Annotations: map[string]string{
"group": "core",
},
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 := os.WriteFile(filepath, b, 0655); err != nil {
if err := cmdxConfig.Init(&ClientConfig{}); err != nil {
return err
}
fmt.Printf("config created: %v", filepath)

fmt.Printf("config created: %v\n", cmdxConfig.File())
printer.SuccessIcon()
return nil
},
}
}

func readConfig() (*configuration, error) {
var c configuration
filepath := fmt.Sprintf("%v.%v", FileName, FileExtension)
b, err := os.ReadFile(filepath)
if err != nil {
return nil, err
}
func configListCommand(cmdxConfig *cmdx.Config) *cobra.Command {
var cmd = &cobra.Command{
Use: "list",
Short: "List client configuration settings",
Example: heredoc.Doc(`
$ siren config list
`),
Annotations: map[string]string{
"group": "core",
},
RunE: func(cmd *cobra.Command, args []string) error {
data, err := cmdxConfig.Read()
if err != nil {
return ErrClientConfigNotFound
}

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

return &c, nil
return cmd
}
14 changes: 14 additions & 0 deletions cli/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cli

import (
"github.com/MakeNowJust/heredoc"
"github.com/odpf/siren/pkg/errors"
)

var (
ErrClientConfigNotFound = errors.New(heredoc.Doc(`
Siren client config not found.
Run "siren config init" to initialize a new client config or
Run "siren help environment" for more information.
`))
)
13 changes: 13 additions & 0 deletions cli/help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cli

import "github.com/MakeNowJust/heredoc"

var envHelp = map[string]string{
"short": "List of supported environment variables",
"long": heredoc.Doc(`
ODPF_CONFIG_DIR: the directory where siren will store configuration files. Default:
"$XDG_CONFIG_HOME/odpf" or "$HOME/.config/odpf".
NO_COLOR: set to any value to avoid printing ANSI escape sequences for color output.
CLICOLOR: set to "0" to disable printing ANSI colors in output.
`),
}
39 changes: 0 additions & 39 deletions cli/migrate.go

This file was deleted.

Loading