Skip to content

Commit

Permalink
Add list command to save corgi paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Andriiklymiuk committed May 18, 2024
1 parent 613dbab commit 1eb855a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
57 changes: 57 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"andriiklymiuk/corgi/utils"
"fmt"
"time"

"github.com/briandowns/spinner"
"github.com/spf13/cobra"
)

var cleanList bool

var listCmd = &cobra.Command{
Use: "list",
Short: "List all executed corgi-compose paths",
Long: `This command lists all the paths to corgi-compose files that have been executed.`,
Run: listRun,
}

func init() {
listCmd.Flags().BoolVar(&cleanList, "cleanList", false, "Clear all the listed paths")
rootCmd.AddCommand(listCmd)
}

func listRun(cmd *cobra.Command, args []string) {
if cleanList {
if err := utils.ClearExecPaths(); err != nil {
fmt.Printf("Error clearing executed paths: %s\n", err)
return
}
fmt.Println("All executed paths have been cleared.")
return
}

s := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
s.Start()
defer s.Stop()

paths, err := utils.ListExecPaths()
if err != nil {
fmt.Printf("Error retrieving executed paths: %s\n", err)
return
}

s.Stop()

if len(paths) == 0 {
fmt.Println("No executed global corgi paths found.")
return
}

fmt.Println("Globally executed corgi paths:")
for _, path := range paths {
fmt.Println(path)
}
}
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,11 @@ func init() {
false,
"Run corgi once and exit",
)
rootCmd.PersistentFlags().BoolP(
"global",
"g",
false,
"Use global path to one of the services",
)
rootCmd.SetVersionTemplate("corgi version {{.Version}}\nChangelog: https://github.com/Andriiklymiuk/corgi/releases/tag/v{{.Version}}\n")
}
11 changes: 2 additions & 9 deletions cmd/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"andriiklymiuk/corgi/utils"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -37,7 +38,7 @@ func upgradeRun(cmd *cobra.Command, args []string) {
fmt.Println("Current version:", currentVersion)
fmt.Println("Latest version available:", latestVersion)

brewPath, err := getHomebrewBinPath()
brewPath, err := utils.GetHomebrewBinPath()
if err != nil {
fmt.Println("Error determining Homebrew binary path:", err)
return
Expand Down Expand Up @@ -77,14 +78,6 @@ func upgradeCorgi() error {
return cmd.Run()
}

func getHomebrewBinPath() (string, error) {
cmd := exec.Command("brew", "--prefix")
output, err := cmd.Output()
if err != nil {
return "", err
}
return fmt.Sprintf("%s/bin", strings.TrimSpace(string(output))), nil
}

func getLatestGitHubTag() (string, error) {
client := &http.Client{}
Expand Down
40 changes: 40 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func GetCorgiServices(cobra *cobra.Command) (*CorgiCompose, error) {
}
CorgiComposePath = pathToCorgiComposeFile

if err := SaveExecPath(pathToCorgiComposeFile); err != nil {
return nil, fmt.Errorf("failed to save corgi-compose file path: %v", err)
}

describeFlag, err := cobra.Root().Flags().GetBool("describe")
if err != nil {
return nil, err
Expand Down Expand Up @@ -464,6 +468,21 @@ func getCorgiConfigFromAlert() (string, error) {
}

func determineCorgiComposePath(cobraCmd *cobra.Command) (string, error) {
globalFlag, err := cobraCmd.Flags().GetBool("global")
if err != nil {
return "", fmt.Errorf("error checking global flag: %v", err)
}

if globalFlag {
globalPath, err := selectGlobalExecPath()
if err != nil {
fmt.Println("No global corgi path selected.", err)
} else if globalPath == "" {
fmt.Println("No global corgi path selected.")
} else {
return globalPath, nil
}
}
filenameFlag, err := cobraCmd.Root().Flags().GetString("filename")
if err != nil {
return "", err
Expand Down Expand Up @@ -496,3 +515,24 @@ func determineCorgiComposePath(cobraCmd *cobra.Command) (string, error) {
return chosenPathToCorgiCompose, nil

}

func selectGlobalExecPath() (string, error) {
paths, err := ListExecPaths()
if err != nil {
return "", fmt.Errorf("error retrieving executed paths: %v", err)
}
if len(paths) == 0 {
return "", fmt.Errorf("no global corgi paths found")
}

selectedPath, err := PickItemFromListPrompt(
"Select a path from global corgi paths",
paths,
"none",
)
if err != nil {
return "", fmt.Errorf("error selecting path: %v", err)
}

return selectedPath, nil
}

0 comments on commit 1eb855a

Please sign in to comment.