From 977f10eccbe0975557a3a0c5173e1bc9d0618a2d Mon Sep 17 00:00:00 2001 From: Dries De Peuter Date: Sat, 30 Dec 2023 11:07:02 +0100 Subject: [PATCH] feat: Add version and update command --- cmd/ph/main.go | 10 ++++++++++ internal/pkg/command/clone.go | 7 +------ internal/pkg/command/config.go | 11 +++++++++-- internal/pkg/command/go.go | 34 +++----------------------------- internal/pkg/command/root.go | 2 ++ internal/pkg/command/update.go | 35 +++++++++++++++++++++++++++++++++ internal/pkg/command/version.go | 32 ++++++++++++++++++++++++++++++ internal/pkg/repo/discover.go | 34 ++++++++++++++++++++++++++++++++ main.go | 20 ++++++++++++++++++- 9 files changed, 145 insertions(+), 40 deletions(-) create mode 100644 internal/pkg/command/update.go create mode 100644 internal/pkg/command/version.go create mode 100644 internal/pkg/repo/discover.go mode change 120000 => 100644 main.go diff --git a/cmd/ph/main.go b/cmd/ph/main.go index 0c90373..2fa1558 100644 --- a/cmd/ph/main.go +++ b/cmd/ph/main.go @@ -2,6 +2,16 @@ package main import "github.com/nousefreak/projecthelper/internal/pkg/command" + +var ( + Version = "dev" + Commit = "none" + Date = "unknown" +) + func main() { + command.Version = Version + command.Commit = Commit + command.Date = Date command.Execute() } diff --git a/internal/pkg/command/clone.go b/internal/pkg/command/clone.go index 491f699..8e13fcf 100644 --- a/internal/pkg/command/clone.go +++ b/internal/pkg/command/clone.go @@ -10,7 +10,6 @@ import ( "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" giturls "github.com/whilp/git-urls" ) @@ -21,6 +20,7 @@ func getCloneCmd() *cobra.Command { Long: `clone command`, Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { + baseDir := getBaseDir() repoURL, err := giturls.Parse(args[0]) if err != nil { logrus.Fatal(fmt.Sprintf("Error cleaning repo URL: %s", err)) @@ -33,11 +33,6 @@ func getCloneCmd() *cobra.Command { if err != nil { logrus.Fatal(fmt.Sprintf("Error making git URL: %s", err)) } - baseDir := viper.GetString("basedir") - if baseDir == "" { - logrus.Fatal("Basedir not set. Run `ph setup` to set it.") - } - targetDir := strings.ToLower(filepath.Join(baseDir, repoPath)) if stat, err := os.Stat(targetDir); err == nil && stat.IsDir() { diff --git a/internal/pkg/command/config.go b/internal/pkg/command/config.go index e16531f..cebe352 100644 --- a/internal/pkg/command/config.go +++ b/internal/pkg/command/config.go @@ -15,7 +15,7 @@ func initConfig() { os.Exit(1) } viper.SetConfigName("config") - viper.SetConfigType("yaml") + viper.SetConfigType("yaml") viper.AddConfigPath(home + "/.config/projecthelper") if cfgFile != "" { @@ -24,7 +24,7 @@ func initConfig() { if err := viper.ReadInConfig(); err != nil { logrus.Warn(err) - cfgFile = home + "/.config/projecthelper/config.yaml" + cfgFile = home + "/.config/projecthelper/config.yaml" if err := os.MkdirAll(filepath.Dir(cfgFile), 0755); err != nil { logrus.Error(err) os.Exit(1) @@ -37,3 +37,10 @@ func initConfig() { } } +func getBaseDir() string { + baseDir := viper.GetString("basedir") + if baseDir == "" { + logrus.Fatal("Basedir not set. Run `ph setup` to set it.") + } + return baseDir +} diff --git a/internal/pkg/command/go.go b/internal/pkg/command/go.go index d00425d..ee78336 100644 --- a/internal/pkg/command/go.go +++ b/internal/pkg/command/go.go @@ -2,14 +2,13 @@ package command import ( "fmt" - "os" "path/filepath" "strings" "github.com/ktr0731/go-fuzzyfinder" + "github.com/nousefreak/projecthelper/internal/pkg/repo" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/viper" ) func getGoCmd() *cobra.Command { @@ -17,11 +16,11 @@ func getGoCmd() *cobra.Command { Use: "go", Short: "go to a project", Run: func(cmd *cobra.Command, args []string) { - baseDir := viper.GetString("basedir") + baseDir := getBaseDir() if baseDir == "" { logrus.Fatal("Basedir not set. Run `ph setup` to set it.") } - paths, err := getRepoPaths(baseDir) + paths, err := repo.GetRepoPaths(baseDir) if err != nil { logrus.Fatal(fmt.Errorf("failed to get repo paths: %w", err)) } @@ -47,30 +46,3 @@ func getGoCmd() *cobra.Command { return cmd } -func getRepoPaths(baseDir string) ([]string, error) { - - result := []string{} - if err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return fmt.Errorf("failed to walk path %s: %w", path, err) - } - if !info.IsDir() { - return nil - } - relPath, err := filepath.Rel(baseDir, path) - if err != nil { - return fmt.Errorf("failed to get relative path for %s: %w", path, err) - } - if info.Name() != ".git" { - return nil - } - - result = append(result, filepath.Dir(relPath)) - - return err - }); err != nil { - return nil, err - } - - return result, nil -} diff --git a/internal/pkg/command/root.go b/internal/pkg/command/root.go index a7c95eb..cf66cfe 100644 --- a/internal/pkg/command/root.go +++ b/internal/pkg/command/root.go @@ -32,6 +32,8 @@ func Execute() { rootCmd.AddCommand(getGoCmd()) rootCmd.AddCommand(getInstallCmd()) rootCmd.AddCommand(getSetupCmd()) + rootCmd.AddCommand(getUpdateCmd()) + rootCmd.AddCommand(getVersionCmd()) cobra.OnInitialize(initConfig) diff --git a/internal/pkg/command/update.go b/internal/pkg/command/update.go new file mode 100644 index 0000000..c867ab5 --- /dev/null +++ b/internal/pkg/command/update.go @@ -0,0 +1,35 @@ +package command + +import ( + "fmt" + "path/filepath" + "strings" + + "github.com/nousefreak/projecthelper/internal/pkg/repo" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func getUpdateCmd() *cobra.Command { + updateCmd := &cobra.Command{ + Use: "update", + Short: "update command", + Long: `update command`, + Run: func(cmd *cobra.Command, args []string) { + baseDir := getBaseDir() + repoPaths, err := repo.GetRepoPaths(baseDir) + if err != nil { + logrus.Fatal(fmt.Errorf("failed to get repo paths: %w", err)) + } + + cmds := []string{} + for _, repoPath := range repoPaths { + cmds = append(cmds, fmt.Sprintf("(echo \"Updating %s\" && cd %s && git fetch -q)", repoPath, filepath.Join(baseDir, repoPath))) + } + + fmt.Fprint(CmdOutput, strings.Join(cmds, " ; ")) + }, + } + return updateCmd +} + diff --git a/internal/pkg/command/version.go b/internal/pkg/command/version.go new file mode 100644 index 0000000..1a74a3d --- /dev/null +++ b/internal/pkg/command/version.go @@ -0,0 +1,32 @@ +package command + +import ( + "runtime/debug" + + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + + +var ( + Version string + Commit string + Date string +) + +func getVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Print the version number", + Run: func(cmd *cobra.Command, args []string) { + if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" { + Version = info.Main.Version + Commit = info.Main.Sum + } + logrus.Infof("version: \t %s", Version) + logrus.Infof("commit: \t %s", Commit) + logrus.Infof("date: \t %s", Date) + }, + } +} + diff --git a/internal/pkg/repo/discover.go b/internal/pkg/repo/discover.go new file mode 100644 index 0000000..132e0a9 --- /dev/null +++ b/internal/pkg/repo/discover.go @@ -0,0 +1,34 @@ +package repo + +import ( + "fmt" + "os" + "path/filepath" +) + +func GetRepoPaths(baseDir string) ([]string, error) { + result := []string{} + if err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return fmt.Errorf("failed to walk path %s: %w", path, err) + } + if !info.IsDir() { + return nil + } + relPath, err := filepath.Rel(baseDir, path) + if err != nil { + return fmt.Errorf("failed to get relative path for %s: %w", path, err) + } + if info.Name() != ".git" { + return nil + } + + result = append(result, filepath.Dir(relPath)) + + return err + }); err != nil { + return nil, err + } + + return result, nil +} diff --git a/main.go b/main.go deleted file mode 120000 index 989939d..0000000 --- a/main.go +++ /dev/null @@ -1 +0,0 @@ -cmd/ph/main.go \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..667ae98 --- /dev/null +++ b/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "github.com/nousefreak/projecthelper/internal/pkg/command" +) + +var ( + version = "dev" + commit = "none" + date = "unknown" +) + +func main() { + command.Version = version + command.Commit = commit + command.Date = date + + command.Execute() +}