Skip to content

Commit

Permalink
Fix argument parsing bugs by using dedicated Viper instances (#60)
Browse files Browse the repository at this point in the history
* Dedicated Viper instances

* Fix azure/cli.go

* Fix chocolatey/cli.go

* Fix cli-docs-build/cli.go

* Fix docs-build/cli.go

* Fix homebrew/cli.go

* Fix winget/cli.go

* Fix dispatch/cli.go

* Fix get/lastest_plugin/cli.go

* Simplify
  • Loading branch information
t0yv0 authored Jan 4, 2023
1 parent 575a272 commit 8dea303
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 38 deletions.
4 changes: 3 additions & 1 deletion cmd/pulumictl/convert-version/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
Expand All @@ -15,6 +15,8 @@ var (
)

func Command() *cobra.Command {
viper := viperlib.New()

command := &cobra.Command{
Use: "convert-version",
Short: "Convert versions",
Expand Down
9 changes: 4 additions & 5 deletions cmd/pulumictl/create/azure/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import (
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
org string
repo string
ref string
tokenClient *http.Client
)

Expand All @@ -29,6 +27,7 @@ type Payload struct {
const eventType = "oss-sdk"

func Command() *cobra.Command {
viper := viperlib.New()
command := &cobra.Command{
Use: "oss-sdk [gitRef]",
Short: "Publish the Azure Nextgen Provider SDK",
Expand All @@ -37,9 +36,9 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")
org = viper.GetString("org")
ref = viper.GetString("ref")

containerRepo := "pulumi/pulumi-azure-nextgen"
ref := args[0]

Expand Down
7 changes: 3 additions & 4 deletions cmd/pulumictl/create/chocolatey/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ import (
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
org string
repo string
ref string
tokenClient *http.Client
app string
)
Expand All @@ -30,6 +29,7 @@ type Payload struct {
const eventType = "choco-deploy"

func Command() *cobra.Command {
viper := viperlib.New()
command := &cobra.Command{
Use: "choco-deploy [tag]",
Short: "Create a Chocolatey Deployment",
Expand All @@ -38,9 +38,8 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken = viperlib.GetString("token")
org = viper.GetString("org")
ref = viper.GetString("ref")
containerRepo := "pulumi/pulumi-chocolatey"
ref := args[0]

Expand Down
8 changes: 3 additions & 5 deletions cmd/pulumictl/create/cli-docs-build/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import (
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
org string
repo string
ref string
tokenClient *http.Client
)

Expand All @@ -30,6 +28,7 @@ type Payload struct {
const eventType = "pulumi-cli"

func Command() *cobra.Command {
viper := viperlib.New()
command := &cobra.Command{
Use: "cli-docs-build [tag]",
Short: "Create a docs build",
Expand All @@ -38,9 +37,8 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")
org = viper.GetString("org")
ref = viper.GetString("ref")
docsRepo := viper.GetString("docs-repo")
ref := args[0]

Expand Down
8 changes: 3 additions & 5 deletions cmd/pulumictl/create/docs-build/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ import (
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
org string
repo string
ref string
category string
displayName string
component bool
Expand All @@ -44,6 +42,7 @@ type Payload struct {
const eventType = "resource-provider"

func Command() *cobra.Command {
viper := viperlib.New()
command := &cobra.Command{
Use: "docs-build [provider] [tag]",
Short: "Create a docs build",
Expand All @@ -52,9 +51,8 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")
org = viper.GetString("org")
ref = viper.GetString("ref")
docsRepo := "pulumi/registry"
project := args[0]
ref := args[1]
Expand Down
8 changes: 4 additions & 4 deletions cmd/pulumictl/create/homebrew/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ import (
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
org string
repo string
ref string
commitsha string
tokenClient *http.Client
)
Expand All @@ -31,6 +29,8 @@ type Payload struct {
const eventType = "homebrew-bump"

func Command() *cobra.Command {
viper := viperlib.New()

command := &cobra.Command{
Use: "homebrew-bump [tag]",
Short: "Create a Homebrew deployment",
Expand All @@ -39,7 +39,7 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")
org = viper.GetString("org")
homebrewRepo := "pulumi/pulumi"
ref := args[0]
Expand Down
5 changes: 2 additions & 3 deletions cmd/pulumictl/create/winget/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"github.com/google/go-github/v32/github"
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
tokenClient *http.Client
)

Expand All @@ -28,7 +27,7 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")
containerRepo := "pulumi/pulumi-winget"
// perform some string manipulation and validation
containerRepoArray := strings.Split(containerRepo, "/")
Expand Down
8 changes: 4 additions & 4 deletions cmd/pulumictl/dispatch/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ import (
gh "github.com/pulumi/pulumictl/pkg/github"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
githubToken string
org string
repo string
ref string
Expand All @@ -28,6 +27,8 @@ type Payload struct {
}

func Command() *cobra.Command {
viper := viperlib.New()

command := &cobra.Command{
Use: "dispatch [ref]",
Short: "Send a command dispatch event with a ref",
Expand All @@ -36,9 +37,8 @@ func Command() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {

// Grab all the configuration variables
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")
org = viper.GetString("org")
ref = viper.GetString("ref")
repo := viper.GetString("repo")
command := viper.GetString("command")
ref := args[0]
Expand Down
5 changes: 2 additions & 3 deletions cmd/pulumictl/get/latest_plugin/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (

"github.com/pulumi/pulumictl/pkg/pluginversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
version string
exists bool
tagsToCheck []string
githubToken string
)

func Command() *cobra.Command {
Expand All @@ -30,7 +29,7 @@ func Command() *cobra.Command {
org, _ := cmd.Flags().GetString("org")
numOfTagsToCheck, _ := cmd.Flags().GetInt("num-tags")
project := args[0]
githubToken = viper.GetString("token")
githubToken := viperlib.GetString("token")

// create a github client and token
ctx, client := gh.CreateGithubClient(githubToken)
Expand Down
3 changes: 2 additions & 1 deletion cmd/pulumictl/get/version/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/pulumi/pulumictl/pkg/gitversion"
"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"
)

var (
Expand All @@ -22,6 +22,7 @@ var (
)

func Command() *cobra.Command {
viper := viperlib.New()
command := &cobra.Command{
Use: "version",
Short: "Calculate versions",
Expand Down
5 changes: 4 additions & 1 deletion cmd/pulumictl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
download_binary "github.com/pulumi/pulumictl/cmd/pulumictl/download-binary"

"github.com/spf13/cobra"
"github.com/spf13/viper"
viperlib "github.com/spf13/viper"

convert_version "github.com/pulumi/pulumictl/cmd/pulumictl/convert-version"
"github.com/pulumi/pulumictl/cmd/pulumictl/copyright"
Expand All @@ -27,6 +27,9 @@ var (
)

func configureCLI() *cobra.Command {
// Using the shared global Viper instance for the top-level command. Sub-commands should use viperlib.New().
viper := viperlib.GetViper()

rootCommand := &cobra.Command{
Use: "pulumictl",
Long: "A swiss army knife for Pulumi development",
Expand Down
5 changes: 3 additions & 2 deletions pkg/gitversion/gitversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/spf13/viper"
viperlib "github.com/spf13/viper"

"github.com/blang/semver"
"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -373,7 +373,8 @@ func mostRecentTag(repo *git.Repository, ref plumbing.Hash, isPrerelease bool,
// workTreeIsDirty returns whether the worktree associated with the given repository
// has local modifications.
func workTreeIsDirty(repo *git.Repository) (bool, error) {
debug := viper.GetBool("debug")
// Using global viper state as "debug" is defined on the global Viper in main.go.
debug := viperlib.GetBool("debug")
workTree, err := repo.Worktree()
if err != nil {
return false, fmt.Errorf("looking up worktree: %w", err)
Expand Down

0 comments on commit 8dea303

Please sign in to comment.