Skip to content

Commit

Permalink
Merge pull request #1086 from twpayne/upgrade-cmd
Browse files Browse the repository at this point in the history
Restore upgrade command
  • Loading branch information
twpayne committed Mar 22, 2021
2 parents 5f43656 + e35be17 commit c500b8a
Show file tree
Hide file tree
Showing 8 changed files with 653 additions and 55 deletions.
68 changes: 34 additions & 34 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ type Config struct {
state stateCmdConfig
status statusCmdConfig
update updateCmdConfig
upgrade upgradeCmdConfig
verify verifyCmdConfig

// Computed configuration.
Expand Down Expand Up @@ -911,40 +912,39 @@ func (c *Config) newRootCmd() (*cobra.Command, error) {
}

rootCmd.SetHelpCommand(c.newHelpCmd())
for _, newCmdFunc := range []func() *cobra.Command{
c.newAddCmd,
c.newApplyCmd,
c.newArchiveCmd,
c.newCatCmd,
c.newCDCmd,
c.newChattrCmd,
c.newCompletionCmd,
c.newDataCmd,
c.newDiffCmd,
c.newDocsCmd,
c.newDoctorCmd,
c.newDumpCmd,
c.newEditCmd,
c.newEditConfigCmd,
c.newExecuteTemplateCmd,
c.newForgetCmd,
c.newGitCmd,
c.newImportCmd,
c.newInitCmd,
c.newManagedCmd,
c.newMergeCmd,
c.newPurgeCmd,
c.newRemoveCmd,
c.newSecretCmd,
c.newSourcePathCmd,
c.newStateCmd,
c.newStatusCmd,
c.newUnmanagedCmd,
c.newUpdateCmd,
c.newVerifyCmd,
} {
rootCmd.AddCommand(newCmdFunc())
}
rootCmd.AddCommand(
c.newAddCmd(),
c.newApplyCmd(),
c.newArchiveCmd(),
c.newCatCmd(),
c.newCDCmd(),
c.newChattrCmd(),
c.newCompletionCmd(),
c.newDataCmd(),
c.newDiffCmd(),
c.newDocsCmd(),
c.newDoctorCmd(),
c.newDumpCmd(),
c.newEditCmd(),
c.newEditConfigCmd(),
c.newExecuteTemplateCmd(),
c.newForgetCmd(),
c.newGitCmd(),
c.newImportCmd(),
c.newInitCmd(),
c.newManagedCmd(),
c.newMergeCmd(),
c.newPurgeCmd(),
c.newRemoveCmd(),
c.newSecretCmd(),
c.newSourcePathCmd(),
c.newStateCmd(),
c.newStatusCmd(),
c.newUnmanagedCmd(),
c.newUpdateCmd(),
c.newUpgradeCmd(),
c.newVerifyCmd(),
)

return rootCmd, nil
}
Expand Down
23 changes: 2 additions & 21 deletions cmd/githubtemplatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package cmd

import (
"context"
"net/http"
"os"

"github.com/google/go-github/v33/github"
"golang.org/x/oauth2"
)

type gitHubData struct {
client *github.Client
keysCache map[string][]*github.Key
}

Expand All @@ -22,29 +18,14 @@ func (c *Config) gitHubKeysTemplateFunc(user string) []*github.Key {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if c.gitHub.client == nil {
var httpClient *http.Client
for _, key := range []string{
"CHEZMOI_GITHUB_ACCESS_TOKEN",
"GITHUB_ACCESS_TOKEN",
"GITHUB_TOKEN",
} {
if accessToken := os.Getenv(key); accessToken != "" {
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{
AccessToken: accessToken,
}))
break
}
}
c.gitHub.client = github.NewClient(httpClient)
}
gitHubClient := newGitHubClient(ctx)

var allKeys []*github.Key
opts := &github.ListOptions{
PerPage: 100,
}
for {
keys, resp, err := c.gitHub.client.Users.ListKeys(ctx, user, opts)
keys, resp, err := gitHubClient.Users.ListKeys(ctx, user, opts)
if err != nil {
returnTemplateError(err)
return nil
Expand Down
32 changes: 32 additions & 0 deletions cmd/upgradecmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cmd

import (
"github.com/spf13/cobra"
)

type upgradeCmdConfig struct {
method string
owner string
repo string
}

func (c *Config) newUpgradeCmd() *cobra.Command {
upgradeCmd := &cobra.Command{
Use: "upgrade",
Short: "Upgrade chezmoi to the latest released version",
Long: mustLongHelp("upgrade"),
Example: example("upgrade"),
Args: cobra.NoArgs,
RunE: c.runUpgradeCmd,
Annotations: map[string]string{
runsCommands: "true",
},
}

flags := upgradeCmd.Flags()
flags.StringVar(&c.upgrade.method, "method", "", "set method")
flags.StringVar(&c.upgrade.owner, "owner", "twpayne", "set owner")
flags.StringVar(&c.upgrade.repo, "repo", "chezmoi", "set repo")

return upgradeCmd
}
Loading

0 comments on commit c500b8a

Please sign in to comment.