Skip to content

Commit

Permalink
feat: Add version and update command
Browse files Browse the repository at this point in the history
  • Loading branch information
NoUseFreak committed Dec 30, 2023
1 parent 3b06906 commit 977f10e
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 40 deletions.
10 changes: 10 additions & 0 deletions cmd/ph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
7 changes: 1 addition & 6 deletions internal/pkg/command/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
giturls "github.com/whilp/git-urls"
)

Expand All @@ -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))
Expand All @@ -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() {
Expand Down
11 changes: 9 additions & 2 deletions internal/pkg/command/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand All @@ -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)
Expand All @@ -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
}
34 changes: 3 additions & 31 deletions internal/pkg/command/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ 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 {
cmd := &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))
}
Expand All @@ -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
}
2 changes: 2 additions & 0 deletions internal/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func Execute() {
rootCmd.AddCommand(getGoCmd())
rootCmd.AddCommand(getInstallCmd())
rootCmd.AddCommand(getSetupCmd())
rootCmd.AddCommand(getUpdateCmd())
rootCmd.AddCommand(getVersionCmd())

cobra.OnInitialize(initConfig)

Expand Down
35 changes: 35 additions & 0 deletions internal/pkg/command/update.go
Original file line number Diff line number Diff line change
@@ -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
}

32 changes: 32 additions & 0 deletions internal/pkg/command/version.go
Original file line number Diff line number Diff line change
@@ -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)
},
}
}

34 changes: 34 additions & 0 deletions internal/pkg/repo/discover.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion main.go

This file was deleted.

19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 977f10e

Please sign in to comment.