Skip to content

Commit

Permalink
Merge pull request #12 from stackitcloud/hs/init-remove
Browse files Browse the repository at this point in the history
Replace inits with constructors
  • Loading branch information
hcsa73 authored Nov 21, 2023
2 parents 48e5e33 + cc3436d commit 6e2ef60
Show file tree
Hide file tree
Showing 45 changed files with 1,282 additions and 1,228 deletions.
2 changes: 1 addition & 1 deletion golang-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ linters:
# additional linters
- errorlint
- exportloopref
# - gochecknoinits
- gochecknoinits
- gocritic
- gofmt
- goimports
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,48 +27,48 @@ type flagModel struct {
JwksCustomEndpoint string
}

var Cmd = &cobra.Command{
Use: "activate-service-account",
Short: "Activate service account authentication",
Long: "Activate authentication using service account credentials.\nFor more details on how to configure your service account, check the Authentication section on our documentation (LINK HERE README)",
Example: `$ stackit auth activate-service-account --service-account-key-path path/to/service_account_key.json --private-key-path path/to/private_key.pem`,
RunE: func(cmd *cobra.Command, args []string) error {
model := parseFlags(cmd)

err := storeFlags(model)
if err != nil {
return err
}

cfg := &sdkConfig.Configuration{
Token: model.ServiceAccountToken,
ServiceAccountKeyPath: model.ServiceAccountKeyPath,
PrivateKeyPath: model.PrivateKeyPath,
TokenCustomUrl: model.TokenCustomEndpoint,
JWKSCustomUrl: model.JwksCustomEndpoint,
}

// Setup authentication based on the provided credentials and the environment
// Initializes the authentication flow
rt, err := sdkAuth.SetupAuth(cfg)
if err != nil {
return fmt.Errorf("set up authentication: %w", err)
}

// Authenticates the service account and stores credentials
email, err := auth.AuthenticateServiceAccount(rt)
if err != nil {
return fmt.Errorf("authenticate service account: %w", err)
}

cmd.Printf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email)

return nil
},
}

func init() {
configureFlags(Cmd)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "activate-service-account",
Short: "Activate service account authentication",
Long: "Activate authentication using service account credentials.\nFor more details on how to configure your service account, check the Authentication section on our documentation (LINK HERE README)",
Example: `$ stackit auth activate-service-account --service-account-key-path path/to/service_account_key.json --private-key-path path/to/private_key.pem`,
RunE: func(cmd *cobra.Command, args []string) error {
model := parseFlags(cmd)

err := storeFlags(model)
if err != nil {
return err
}

cfg := &sdkConfig.Configuration{
Token: model.ServiceAccountToken,
ServiceAccountKeyPath: model.ServiceAccountKeyPath,
PrivateKeyPath: model.PrivateKeyPath,
TokenCustomUrl: model.TokenCustomEndpoint,
JWKSCustomUrl: model.JwksCustomEndpoint,
}

// Setup authentication based on the provided credentials and the environment
// Initializes the authentication flow
rt, err := sdkAuth.SetupAuth(cfg)
if err != nil {
return fmt.Errorf("set up authentication: %w", err)
}

// Authenticates the service account and stores credentials
email, err := auth.AuthenticateServiceAccount(rt)
if err != nil {
return fmt.Errorf("authenticate service account: %w", err)
}

cmd.Printf("You have been successfully authenticated to the STACKIT CLI!\nService account email: %s\n", email)

return nil
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
Expand Down
21 changes: 12 additions & 9 deletions internal/cmd/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import (
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "auth",
Short: "Provides authentication functionality",
Long: "Provides authentication functionality",
Example: `$ stackit auth login`,
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "auth",
Short: "Provides authentication functionality",
Long: "Provides authentication functionality",
Example: `$ stackit auth login`,
}
addSubcommands(cmd)
return cmd
}

func init() {
// Add all direct child commands
Cmd.AddCommand(login.Cmd)
Cmd.AddCommand(activateserviceaccount.Cmd)
func addSubcommands(cmd *cobra.Command) {
cmd.AddCommand(login.NewCmd())
cmd.AddCommand(activateserviceaccount.NewCmd())
}
32 changes: 16 additions & 16 deletions internal/cmd/auth/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ import (
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "login",
Short: "Login to the provider",
Long: "Login to the provider",
Example: `$ stackit auth login`,
RunE: func(cmd *cobra.Command, args []string) error {
err := auth.AuthorizeUser()
if err != nil {
return fmt.Errorf("authorization failed: %w", err)
}
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "login",
Short: "Login to the provider",
Long: "Login to the provider",
Example: `$ stackit auth login`,
RunE: func(cmd *cobra.Command, args []string) error {
err := auth.AuthorizeUser()
if err != nil {
return fmt.Errorf("authorization failed: %w", err)
}

cmd.Println("Successfully logged into STACKIT CLI.")
return nil
},
}

func init() {
cmd.Println("Successfully logged into STACKIT CLI.")
return nil
},
}
return cmd
}
22 changes: 13 additions & 9 deletions internal/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,19 @@ import (
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "config",
Short: "CLI configuration options",
Long: "CLI configuration options",
Example: fmt.Sprintf("%s\n%s\n%s", set.Cmd.Example, inspect.Cmd.Example, unset.Cmd.Example),
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: "CLI configuration options",
Long: "CLI configuration options",
Example: fmt.Sprintf("%s\n%s\n%s", set.NewCmd().Example, inspect.NewCmd().Example, unset.NewCmd().Example),
}
addSubcommands(cmd)
return cmd
}

func init() {
Cmd.AddCommand(inspect.Cmd)
Cmd.AddCommand(set.Cmd)
Cmd.AddCommand(unset.Cmd)
func addSubcommands(cmd *cobra.Command) {
cmd.AddCommand(inspect.NewCmd())
cmd.AddCommand(set.NewCmd())
cmd.AddCommand(unset.NewCmd())
}
39 changes: 21 additions & 18 deletions internal/cmd/config/inspect/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@ import (
"github.com/spf13/viper"
)

var Cmd = &cobra.Command{
Use: "inspect",
Short: "Inspect the current CLI configuration values",
Long: "Inspect the current CLI configuration values",
Example: `$ stackit config inspect`,
RunE: func(cmd *cobra.Command, args []string) error {
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("read config file: %w", err)
}
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "inspect",
Short: "Inspect the current CLI configuration values",
Long: "Inspect the current CLI configuration values",
Example: `$ stackit config inspect`,
RunE: func(cmd *cobra.Command, args []string) error {
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("read config file: %w", err)
}

configData := viper.AllSettings()
configData := viper.AllSettings()

configJSON, err := json.MarshalIndent(configData, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
cmd.Println(string(configJSON))
configJSON, err := json.MarshalIndent(configData, "", " ")
if err != nil {
return fmt.Errorf("marshal config: %w", err)
}
cmd.Println(string(configJSON))

return nil
},
return nil
},
}
return cmd
}
52 changes: 26 additions & 26 deletions internal/cmd/config/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ type flagModel struct {
SessionTimeLimit *string
}

var Cmd = &cobra.Command{
Use: "set",
Short: "Set CLI configuration options",
Long: "Set CLI configuration options",
Example: `$ stackit config set --project-id xxx`,
RunE: func(cmd *cobra.Command, args []string) error {
model, err := parseFlags(cmd)
if err != nil {
return err
}

if model.SessionTimeLimit != nil {
cmd.Println("Authenticate again to apply changes to session time limit")
viper.Set(config.SessionTimeLimitKey, *model.SessionTimeLimit)
}

err = viper.WriteConfig()
if err != nil {
return fmt.Errorf("write new config to file: %w", err)
}
return nil
},
}

func init() {
configureFlags(Cmd)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "set",
Short: "Set CLI configuration options",
Long: "Set CLI configuration options",
Example: `$ stackit config set --project-id xxx`,
RunE: func(cmd *cobra.Command, args []string) error {
model, err := parseFlags(cmd)
if err != nil {
return err
}

if model.SessionTimeLimit != nil {
cmd.Println("Authenticate again to apply changes to session time limit")
viper.Set(config.SessionTimeLimitKey, *model.SessionTimeLimit)
}

err = viper.WriteConfig()
if err != nil {
return fmt.Errorf("write new config to file: %w", err)
}
return nil
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
Expand Down
58 changes: 29 additions & 29 deletions internal/cmd/config/unset/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@ type flagModel struct {
SKECustomEndpoint bool
}

var Cmd = &cobra.Command{
Use: "unset",
Short: "Unset CLI configuration options",
Long: "Unset CLI configuration options",
Example: `$ stackit config unset --project-id`,
RunE: func(cmd *cobra.Command, args []string) error {
model := parseFlags(cmd)
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "unset",
Short: "Unset CLI configuration options",
Long: "Unset CLI configuration options",
Example: `$ stackit config unset --project-id`,
RunE: func(cmd *cobra.Command, args []string) error {
model := parseFlags(cmd)

if model.ProjectId {
viper.Set(config.ProjectIdKey, "")
}
if model.DNSCustomEndpoint {
viper.Set(config.DNSCustomEndpointKey, "")
}
if model.PostgreSQLCustomEndpoint {
viper.Set(config.PostgreSQLCustomEndpointKey, "")
}
if model.PostgreSQLCustomEndpoint {
viper.Set(config.SKECustomEndpointKey, "")
}
if model.ProjectId {
viper.Set(config.ProjectIdKey, "")
}
if model.DNSCustomEndpoint {
viper.Set(config.DNSCustomEndpointKey, "")
}
if model.PostgreSQLCustomEndpoint {
viper.Set(config.PostgreSQLCustomEndpointKey, "")
}
if model.PostgreSQLCustomEndpoint {
viper.Set(config.SKECustomEndpointKey, "")
}

err := viper.WriteConfig()
if err != nil {
return fmt.Errorf("write updated config to file: %w", err)
}
return nil
},
}

func init() {
configureFlags(Cmd)
err := viper.WriteConfig()
if err != nil {
return fmt.Errorf("write updated config to file: %w", err)
}
return nil
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
Expand Down
20 changes: 12 additions & 8 deletions internal/cmd/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import (
"github.com/spf13/cobra"
)

var Cmd = &cobra.Command{
Use: "dns",
Short: "Provides functionality for DNS",
Long: "Provides functionality for DNS",
Example: fmt.Sprintf("%s\n%s", zone.Cmd.Example, recordset.Cmd.Example),
func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dns",
Short: "Provides functionality for DNS",
Long: "Provides functionality for DNS",
Example: fmt.Sprintf("%s\n%s", zone.NewCmd().Example, recordset.NewCmd().Example),
}
addSubcommands(cmd)
return cmd
}

func init() {
Cmd.AddCommand(zone.Cmd)
Cmd.AddCommand(recordset.Cmd)
func addSubcommands(cmd *cobra.Command) {
cmd.AddCommand(zone.NewCmd())
cmd.AddCommand(recordset.NewCmd())
}
Loading

0 comments on commit 6e2ef60

Please sign in to comment.