Skip to content

Commit

Permalink
Merge pull request #21 from stackitcloud/hs/global-flags-package
Browse files Browse the repository at this point in the history
Move global flags to common flags package
  • Loading branch information
hcsa73 authored Nov 23, 2023
2 parents 5a86952 + 865fca3 commit 7c4949a
Show file tree
Hide file tree
Showing 57 changed files with 273 additions and 384 deletions.
18 changes: 8 additions & 10 deletions internal/cmd/dns/record-set/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@ import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
)

const (
projectIdFlag = "project-id"
zoneIdFlag = "zone-id"
commentFlag = "comment"
nameFlag = "name"
recordFlag = "record"
ttlFlag = "ttl"
typeFlag = "type"
zoneIdFlag = "zone-id"
commentFlag = "comment"
nameFlag = "name"
recordFlag = "record"
ttlFlag = "ttl"
typeFlag = "type"
)

type flagModel struct {
Expand Down Expand Up @@ -91,7 +89,7 @@ func configureFlags(cmd *cobra.Command) {
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
projectId := globalflags.GetString(globalflags.ProjectIdFlag)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/dns/record-set/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
Expand All @@ -15,6 +14,8 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

var projectIdFlag = globalflags.ProjectIdFlag.FlagName()

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
Expand Down Expand Up @@ -230,15 +231,12 @@ func TestParseFlags(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
cmd := &cobra.Command{}

// Flag defined in root command
err := testutils.ConfigureBindUUIDFlag(cmd, projectIdFlag, config.ProjectIdKey)
configureFlags(cmd)
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flag --%s: %v", projectIdFlag, err)
t.Fatalf("configure global flags: %v", err)
}

configureFlags(cmd)

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/dns/record-set/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
)

const (
projectIdFlag = "project-id"
zoneIdFlag = "zone-id"
recordSetIdFlag = "record-set-id"
)
Expand Down Expand Up @@ -79,7 +77,7 @@ func configureFlags(cmd *cobra.Command) {
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
projectId := globalflags.GetString(globalflags.ProjectIdFlag)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/dns/record-set/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand All @@ -14,6 +13,8 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

var projectIdFlag = globalflags.ProjectIdFlag.FlagName()

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
Expand Down Expand Up @@ -140,15 +141,12 @@ func TestParseFlags(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
cmd := &cobra.Command{}

// Flag defined in root command
err := testutils.ConfigureBindUUIDFlag(cmd, projectIdFlag, config.ProjectIdKey)
configureFlags(cmd)
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flag --%s: %v", projectIdFlag, err)
t.Fatalf("configure global flags: %v", err)
}

configureFlags(cmd)

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/dns/record-set/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import (
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

const (
projectIdFlag = "project-id"
zoneIdFlag = "zone-id"
recordSetIdFlag = "record-set-id"
)
Expand Down Expand Up @@ -77,7 +75,7 @@ func configureFlags(cmd *cobra.Command) {
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
projectId := globalflags.GetString(globalflags.ProjectIdFlag)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/dns/record-set/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand All @@ -14,6 +13,8 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

var projectIdFlag = globalflags.ProjectIdFlag.FlagName()

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
Expand Down Expand Up @@ -140,15 +141,12 @@ func TestParseFlags(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
cmd := &cobra.Command{}

// Flag defined in root command
err := testutils.ConfigureBindUUIDFlag(cmd, projectIdFlag, config.ProjectIdKey)
configureFlags(cmd)
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flag --%s: %v", projectIdFlag, err)
t.Fatalf("configure global flags: %v", err)
}

configureFlags(cmd)

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/dns/record-set/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import (
"fmt"
"strings"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

const (
projectIdFlag = "project-id"
zoneIdFlag = "zone-id"
nameLikeFlag = "name-like"
activeFlag = "is-active"
Expand Down Expand Up @@ -102,7 +100,7 @@ func configureFlags(cmd *cobra.Command) {
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
projectId := globalflags.GetString(globalflags.ProjectIdFlag)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/dns/record-set/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
Expand All @@ -15,6 +14,8 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

var projectIdFlag = globalflags.ProjectIdFlag.FlagName()

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
Expand Down Expand Up @@ -191,15 +192,12 @@ func TestParseFlags(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
cmd := &cobra.Command{}

// Flag defined in root command
err := testutils.ConfigureBindUUIDFlag(cmd, projectIdFlag, config.ProjectIdKey)
configureFlags(cmd)
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flag --%s: %v", projectIdFlag, err)
t.Fatalf("configure global flags: %v", err)
}

configureFlags(cmd)

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/dns/record-set/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@ import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
)

const (
projectIdFlag = "project-id"
zoneIdFlag = "zone-id"
recordSetIdFlag = "record-set-id"
commentFlag = "comment"
Expand Down Expand Up @@ -88,7 +86,7 @@ func configureFlags(cmd *cobra.Command) {
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
projectId := globalflags.GetString(globalflags.ProjectIdFlag)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}
Expand Down
14 changes: 6 additions & 8 deletions internal/cmd/dns/record-set/update/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/google/go-cmp/cmp"
Expand All @@ -15,6 +14,8 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

var projectIdFlag = globalflags.ProjectIdFlag.FlagName()

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
Expand Down Expand Up @@ -212,15 +213,12 @@ func TestParseFlags(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
cmd := &cobra.Command{}

// Flag defined in root command
err := testutils.ConfigureBindUUIDFlag(cmd, projectIdFlag, config.ProjectIdKey)
configureFlags(cmd)
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flag --%s: %v", projectIdFlag, err)
t.Fatalf("configure global flags: %v", err)
}

configureFlags(cmd)

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions internal/cmd/dns/zone/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
)

const (
projectIdFlag = "project-id"
nameFlag = "name"
dnsNameFlag = "dns-name"
defaultTTLFlag = "default-ttl"
Expand Down Expand Up @@ -109,7 +107,7 @@ func configureFlags(cmd *cobra.Command) {
}

func parseFlags(cmd *cobra.Command) (*flagModel, error) {
projectId := viper.GetString(config.ProjectIdKey)
projectId := globalflags.GetString(globalflags.ProjectIdFlag)
if projectId == "" {
return nil, fmt.Errorf("project ID not set")
}
Expand Down
Loading

0 comments on commit 7c4949a

Please sign in to comment.