Skip to content

Commit

Permalink
feat: enhance doctor command
Browse files Browse the repository at this point in the history
  • Loading branch information
mkloubert committed Jan 2, 2025
1 parent e608a5b commit 32436cf
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 32 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log (go-package-manager)

## 0.28.0

- **BREAKING CHANGE**: `bump version` command is now reduced to simple `bump`
- feat: additional checks like environment variables and unsed modules by `doctor` command

## 0.27.0

- **BREAKING CHANGE**: remove `audit` command
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ will run `go build .` in the current directory or the `build` script in [gpm.yam
The simple execution of

```bash
gpm bump version
gpm bump
```

will detect the latest version of the underlying repository from [Git tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging) and then upgrade the minor part only, e.g. `1.2.4` => `1.3.0`. Finally a new Git tag is created in the format `v1.3.0`.
Expand Down
2 changes: 2 additions & 0 deletions aliases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ aliases:
- github.com/charmbracelet/glamour
mongo:
- https://go.mongodb.org/mongo-driver/mongo
now:
- https://github.com/jinzhu/now
pretty:
- https://github.com/jedib0t/go-pretty/v6/list
- https://github.com/jedib0t/go-pretty/v6/progress
Expand Down
44 changes: 13 additions & 31 deletions commands/bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/spf13/cobra"
)

func init_bump_version_command(parentCmd *cobra.Command, app *types.AppContext) {
func Init_Bump_Command(parentCmd *cobra.Command, app *types.AppContext) {
var breaking bool
var feature bool
var fix bool
Expand All @@ -40,9 +40,9 @@ func init_bump_version_command(parentCmd *cobra.Command, app *types.AppContext)
var message string
var patch int64

var versionCmd = &cobra.Command{
Use: "version",
Aliases: []string{"v", "ver"},
var bumpVersionCmd = &cobra.Command{
Use: "bump",
Aliases: []string{"bp", "bmp"},
Short: "Bump version",
Long: `Bumps a version number.`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -68,34 +68,16 @@ func init_bump_version_command(parentCmd *cobra.Command, app *types.AppContext)
},
}

versionCmd.Flags().BoolVarP(&breaking, "breaking", "", false, "increase major part by 1")
versionCmd.Flags().BoolVarP(&feature, "feature", "", false, "increase minor part by 1")
versionCmd.Flags().BoolVarP(&fix, "fix", "", false, "increase patch part by 1")
versionCmd.Flags().BoolVarP(&force, "force", "", false, "ignore value of previous version")
versionCmd.Flags().Int64VarP(&major, "major", "", -1, "set major part")
versionCmd.Flags().StringVarP(&message, "message", "", "", "custom git message")
versionCmd.Flags().Int64VarP(&minor, "minor", "", -1, "set minor part")
versionCmd.Flags().Int64VarP(&patch, "patch", "", -1, "set patch part")

parentCmd.AddCommand(
versionCmd,
)
}

func Init_Bump_Command(parentCmd *cobra.Command, app *types.AppContext) {
var bumpCmd = &cobra.Command{
Use: "bump",
Aliases: []string{"bp", "bmp"},
Short: "Bump resource",
Long: `Bumps a resource like a version.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

init_bump_version_command(bumpCmd, app)
bumpVersionCmd.Flags().BoolVarP(&breaking, "breaking", "", false, "increase major part by 1")
bumpVersionCmd.Flags().BoolVarP(&feature, "feature", "", false, "increase minor part by 1")
bumpVersionCmd.Flags().BoolVarP(&fix, "fix", "", false, "increase patch part by 1")
bumpVersionCmd.Flags().BoolVarP(&force, "force", "", false, "ignore value of previous version")
bumpVersionCmd.Flags().Int64VarP(&major, "major", "", -1, "set major part")
bumpVersionCmd.Flags().StringVarP(&message, "message", "", "", "custom git message")
bumpVersionCmd.Flags().Int64VarP(&minor, "minor", "", -1, "set minor part")
bumpVersionCmd.Flags().Int64VarP(&patch, "patch", "", -1, "set patch part")

parentCmd.AddCommand(
bumpCmd,
bumpVersionCmd,
)
}
45 changes: 45 additions & 0 deletions commands/doctor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"sort"
"strings"
Expand Down Expand Up @@ -192,6 +193,35 @@ func Init_Doctor_Command(parentCmd *cobra.Command, app *types.AppContext) {
}
fmt.Println()

fmt.Println("Checking for unsed dependencies ...")
for i, item := range goMod.Require {
s := spinner.New(spinner.CharSets[24], 100*time.Millisecond)
s.Prefix = "\t["
s.Suffix = fmt.Sprintf("] Checking '%s' (%v/%v) ...", item.Path, i+1, len(goMod.Require))
s.Start()

p := exec.Command("go", "mod", "why", "-m", item.Path)
p.Dir = app.Cwd
p.Stderr = nil
p.Stdin = nil
p.Stdout = nil
output, err := p.Output()

s.Stop()

if err == nil {
strOutput := string(output)
if strings.Contains(strOutput, fmt.Sprintf("module does not need module %s)", item.Path)) {
fmt.Printf("\t[%s] Module '%s' is not used, run 'gpm uninstall %s' or a single 'gpm tidy' to fix this%s", red("!"), item.Path, item.Path, fmt.Sprintln())
} else {
fmt.Printf("\t[%s] '%s' has no known issues%s", green("✓"), item.Path, fmt.Sprintln())
}
} else {
fmt.Printf("\t[%s] Check failed for '%s':%s%s", red("!"), item.Path, err.Error(), fmt.Sprintln())
}
}
fmt.Println()

fmt.Println("Checking all dependencies for security issues ...")
for i, item := range goMod.Require {
s := spinner.New(spinner.CharSets[24], 100*time.Millisecond)
Expand Down Expand Up @@ -379,6 +409,21 @@ func Init_Doctor_Command(parentCmd *cobra.Command, app *types.AppContext) {
fmt.Println()
}
}

fmt.Println("Environment variables ...")
{
vars := make([]string, 0)
vars = append(vars, "GOPATH", "GOROOT", "GOPROXY")

for _, varName := range vars {
varValue := os.Getenv(varName)
if varValue != "" {
fmt.Printf("\t[%s] %s is set: %s%s", green("✓"), varName, varValue, fmt.Sprintln())
} else {
fmt.Printf("\t[%s] %s is not set%s", red("!"), varName, fmt.Sprintln())
}
}
}
},
}

Expand Down

0 comments on commit 32436cf

Please sign in to comment.