Skip to content

Commit

Permalink
Change to functions and combine files
Browse files Browse the repository at this point in the history
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
  • Loading branch information
saswatamcode committed Feb 20, 2022
1 parent 7835f9d commit 969d235
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 192 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ Usage:
obsctl [command]

Available Commands:
current Display configuration for the currently logged in tenant.
context View/Add/Edit context configuration.
help Help about any command
login Login as a tenant. Will also save tenant details locally.
logout Logout currently logged in tenant.
metrics Metrics based operations for Observatorium.
switch Switch to another locally saved tenant.

Flags:
-h, --help help for obsctl
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

func main() {
ctx, cancel := context.WithCancel(context.Background())
cmd := cmd.NewObsctlCmd(ctx)

var g run.Group
g.Add(func() error {
return cmd.Execute(ctx)
return cmd.Execute()
}, func(err error) {
cancel()
})
Expand Down
43 changes: 20 additions & 23 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ const (
logFormatCLILog = "clilog"
)

var logger log.Logger

var logLevel, logFormat string
var logger log.Logger

func setupLogger() {
func setupLogger(*cobra.Command, []string) {
var lvl level.Option
switch logLevel {
case "error":
Expand All @@ -47,26 +46,24 @@ func setupLogger() {
}
}

var rootCmd = &cobra.Command{
Use: "obsctl",
Short: "CLI to interact with Observatorium",
Long: `CLI to interact with Observatorium`,
Version: version.Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// TODO(saswatamcode): Propagte ctx here.
setupLogger()
},
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute(ctx context.Context) error {
if err := rootCmd.Execute(); err != nil {
return err
func NewObsctlCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "obsctl",
Short: "CLI to interact with Observatorium",
Long: `CLI to interact with Observatorium`,
Version: version.Version,
PersistentPreRun: setupLogger,
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "run called")
},
}
return nil
}

func init() {
rootCmd.PersistentFlags().StringVar(&logLevel, "log.level", "info", "Log filtering level.")
rootCmd.PersistentFlags().StringVar(&logFormat, "log.format", logFormatCLILog, "Log format to use.")
cmd.AddCommand(NewMetricsCmd(ctx))
cmd.AddCommand(NewContextCommand(ctx))
cmd.AddCommand(NewLoginCmd(ctx))

cmd.PersistentFlags().StringVar(&logLevel, "log.level", "info", "Log filtering level.")
cmd.PersistentFlags().StringVar(&logFormat, "log.format", logFormatCLILog, "Log format to use.")

return cmd
}
76 changes: 39 additions & 37 deletions pkg/cmd/context.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
package cmd

import (
"context"

"github.com/go-kit/log/level"
"github.com/spf13/cobra"
)

var contextCmd = &cobra.Command{
Use: "context",
Short: "View/Add/Edit context configuration.",
Long: "View/Add/Edit context configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "context called")
},
}
func NewContextCommand(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "context",
Short: "View/Add/Edit context configuration.",
Long: "View/Add/Edit context configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "context called")
},
}

var contextApiCmd = &cobra.Command{
Use: "api",
Short: "Add/edit API configuration.",
Long: "Add/edit API configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "api called")
},
}
var contextSwitchCmd = &cobra.Command{
Use: "switch",
Short: "Switch to another context.",
Long: "View/Add/Edit context configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "switch called")
},
}
var contextCurrentCmd = &cobra.Command{
Use: "current",
Short: "View current context configuration.",
Long: "View current context configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "current called")
},
}
apiCmd := &cobra.Command{
Use: "api",
Short: "Add/edit API configuration.",
Long: "Add/edit API configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "api called")
},
}
switchCmd := &cobra.Command{
Use: "switch",
Short: "Switch to another context.",
Long: "View/Add/Edit context configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "switch called")
},
}
currentCmd := &cobra.Command{
Use: "current",
Short: "View current context configuration.",
Long: "View current context configuration.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "current called")
},
}

func init() {
rootCmd.AddCommand(contextCmd)
cmd.AddCommand(apiCmd)
cmd.AddCommand(switchCmd)
cmd.AddCommand(currentCmd)

contextCmd.AddCommand(contextApiCmd)
contextCmd.AddCommand(contextSwitchCmd)
contextCmd.AddCommand(contextCurrentCmd)
return cmd
}
36 changes: 19 additions & 17 deletions pkg/cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
package cmd

import (
"context"

"github.com/go-kit/log/level"
"github.com/spf13/cobra"
)

var loginCmd = &cobra.Command{
Use: "login",
Short: "Login as a tenant. Will also save tenant details locally.",
Long: "Login as a tenant. Will also save tenant details locally.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "login called")
},
}
func NewLoginCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Login as a tenant. Will also save tenant details locally.",
Long: "Login as a tenant. Will also save tenant details locally.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "login called")
},
}

func init() {
rootCmd.AddCommand(loginCmd)
cmd.Flags().String("tenant", "", "The name of the tenant.")
cmd.Flags().String("api", "", "The URL or name of the Observatorium API.")
cmd.Flags().String("ca", "", "Path to the TLS CA against which to verify the Observatorium API. If no server CA is specified, the client will use the system certificates.")
cmd.Flags().String("oidc.issuer-url", "", "The OIDC issuer URL, see https://openid.net/specs/openid-connect-discovery-1_0.html#IssuerDiscovery.")
cmd.Flags().String("oidc.client-secret", "", "The OIDC client secret, see https://tools.ietf.org/html/rfc6749#section-2.3.")
cmd.Flags().String("oidc.client-id", "", "The OIDC client ID, see https://tools.ietf.org/html/rfc6749#section-2.3.")
cmd.Flags().String("oidc.audience", "", "The audience for whom the access token is intended, see https://openid.net/specs/openid-connect-core-1_0.html#IDToken.")

loginCmd.Flags().String("tenant", "", "The name of the tenant.")
loginCmd.Flags().String("api", "", "The URL or name of the Observatorium API.")
loginCmd.Flags().String("ca", "", "Path to the TLS CA against which to verify the Observatorium API. If no server CA is specified, the client will use the system certificates.")
loginCmd.Flags().String("oidc.issuer-url", "", "The OIDC issuer URL, see https://openid.net/specs/openid-connect-discovery-1_0.html#IssuerDiscovery.")
loginCmd.Flags().String("oidc.client-secret", "", "The OIDC client secret, see https://tools.ietf.org/html/rfc6749#section-2.3.")
loginCmd.Flags().String("oidc.client-id", "", "The OIDC client ID, see https://tools.ietf.org/html/rfc6749#section-2.3.")
loginCmd.Flags().String("oidc.audience", "", "The audience for whom the access token is intended, see https://openid.net/specs/openid-connect-core-1_0.html#IDToken.")
return cmd
}
118 changes: 109 additions & 9 deletions pkg/cmd/metrics.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,119 @@
package cmd

import (
"context"

"github.com/go-kit/log/level"
"github.com/spf13/cobra"
)

var metricsCmd = &cobra.Command{
Use: "metrics",
Short: "Metrics based operations for Observatorium.",
Long: "Metrics based operations for Observatorium.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "metrics called")
},
func NewMetricsGetCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Read series, labels & rules (JSON/YAML) of a tenant.",
Long: "Read series, labels & rules (JSON/YAML) of a tenant.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "get called")
},
}

seriesCmd := &cobra.Command{
Use: "series",
Short: "Get series of a tenant.",
Long: "Get series of a tenant..",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "series called")
},
}

labelsCmd := &cobra.Command{
Use: "labels",
Short: "Get labels of a tenant.",
Long: "Get labels of a tenant.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "labels called")
},
}

labelValuesCmd := &cobra.Command{
Use: "labelvalues",
Short: "Get label values of a tenant.",
Long: "Get label values of a tenant.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "label values called")
},
}

rulesCmd := &cobra.Command{
Use: "rules",
Short: "Get rules of a tenant.",
Long: "Get rules of a tenant.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "rules called")
},
}

rulesRawCmd := &cobra.Command{
Use: "rules.raw",
Short: "Get configured rules of a tenant.",
Long: "Get configured rules of a tenant.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "rules.raw called")
},
}

cmd.AddCommand(seriesCmd)
cmd.AddCommand(labelsCmd)
cmd.AddCommand(labelValuesCmd)
cmd.AddCommand(rulesCmd)
cmd.AddCommand(rulesRawCmd)

return cmd
}

func init() {
rootCmd.AddCommand(metricsCmd)
func NewMetricsSetCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "set",
Short: "Write Prometheus Rules configuration for a tenant.",
Long: "Write Prometheus Rules configuration for a tenant.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "set called")
},
}

cmd.Flags().String("rule.file", "", "Path to Rules configuration file, which will be set for a tenant.")

return cmd
}

func NewMetricsQueryCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "query",
Short: "Query metrics for a tenant.",
Long: "Query metrics for a tenant. Pass a single valid PromQL query to fetch results for.",
Example: `obsctl query "prometheus_http_request_total"`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "query called")
},
}

return cmd
}

func NewMetricsCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "metrics",
Short: "Metrics based operations for Observatorium.",
Long: "Metrics based operations for Observatorium.",
Run: func(cmd *cobra.Command, args []string) {
level.Info(logger).Log("msg", "metrics called")
},
}

cmd.AddCommand(NewMetricsGetCmd(ctx))
cmd.AddCommand(NewMetricsSetCmd(ctx))
cmd.AddCommand(NewMetricsQueryCmd(ctx))

return cmd
}
Loading

0 comments on commit 969d235

Please sign in to comment.