fix: replace go.mod when GoMod empty
Signed-off-by: Young Xu <xuthus5@gmail.com>
This commit is contained in:
parent
0e30bfcb46
commit
c4990d6a85
19
README.md
19
README.md
@ -10,6 +10,25 @@ go install gitter.top/apps/gomod/...@latest
|
|||||||
|
|
||||||
### usage
|
### usage
|
||||||
|
|
||||||
|
```shell
|
||||||
|
x@fedora:~/GolandProjects/gomod$ gomod -h
|
||||||
|
go mod manager
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
gomod [flags]
|
||||||
|
gomod [command]
|
||||||
|
|
||||||
|
Available Commands:
|
||||||
|
analyzed analyzed project dependencies
|
||||||
|
help Help about any command
|
||||||
|
upgrade update project dependencies to latest
|
||||||
|
|
||||||
|
Flags:
|
||||||
|
-h, --help help for gomod
|
||||||
|
|
||||||
|
Use "gomod [command] --help" for more information about a command.
|
||||||
|
```
|
||||||
|
|
||||||
you can show go.mod available updates using `gomod`:
|
you can show go.mod available updates using `gomod`:
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
79
gomod.go
79
gomod.go
@ -6,6 +6,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/briandowns/spinner"
|
"github.com/briandowns/spinner"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/google/go-github/v64/github"
|
"github.com/google/go-github/v64/github"
|
||||||
@ -13,12 +21,6 @@ import (
|
|||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"golang.org/x/mod/modfile"
|
"golang.org/x/mod/modfile"
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -262,15 +264,9 @@ type ModuleError struct {
|
|||||||
|
|
||||||
func Analyzed() {
|
func Analyzed() {
|
||||||
spin := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
spin := spinner.New(spinner.CharSets[14], 100*time.Millisecond)
|
||||||
spin.Suffix = " Checking for updates..."
|
spin.Suffix = " Analyzing for dependencies..."
|
||||||
spin.Start()
|
spin.Start()
|
||||||
|
|
||||||
//modFile, err := GetModFile("go.mod")
|
|
||||||
//if err != nil {
|
|
||||||
// spin.Stop()
|
|
||||||
// logrus.Errorf("get go.mod file failed: %v", err)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
modules, err := analyzed("go", "list", "-m", "-json", "-mod=readonly", "all")
|
modules, err := analyzed("go", "list", "-m", "-json", "-mod=readonly", "all")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
spin.Stop()
|
spin.Stop()
|
||||||
@ -286,23 +282,46 @@ func Analyzed() {
|
|||||||
mod.Path,
|
mod.Path,
|
||||||
getRelation(mod),
|
getRelation(mod),
|
||||||
mod.Version,
|
mod.Version,
|
||||||
mod.GoVersion,
|
getGoVersion(mod),
|
||||||
getToolChains(mod.GoMod),
|
getToolChains(mod),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(tableRows) == 0 {
|
if len(tableRows) == 0 {
|
||||||
awesome := color.New(color.FgHiGreen, color.Bold).Sprint("✔ Awesome!")
|
awesome := color.New(color.FgHiGreen, color.Bold).Sprint("✔ Empty!")
|
||||||
fmt.Printf(" %s All of your dependencies are up-to-date.\n", awesome)
|
fmt.Printf(" %s All of your dependencies are empty.\n", awesome)
|
||||||
} else {
|
} else {
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
table.SetHeader([]string{"Package", "Relation", "Version", "GoVersion", "ToolChains"})
|
table.SetHeader([]string{"Package", "Relation", "Version", "GoVersion", "ToolChains"})
|
||||||
table.SetBorder(false)
|
table.EnableBorder(true)
|
||||||
table.AppendBulk(tableRows)
|
table.AppendBulk(tableRows)
|
||||||
table.Render()
|
table.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getGoVersion(m *Module) string {
|
||||||
|
if m.GoVersion != "" {
|
||||||
|
return m.GoVersion
|
||||||
|
}
|
||||||
|
var mf string
|
||||||
|
if m.GoMod != "" {
|
||||||
|
mf = m.GoMod
|
||||||
|
} else if m.Dir != "" {
|
||||||
|
mf = filepath.Join(m.Dir, "go.mod")
|
||||||
|
}
|
||||||
|
if mf == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
modFile, err := GetModFile(mf)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if modFile.Go != nil {
|
||||||
|
return modFile.Go.Version
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func getRelation(m *Module) string {
|
func getRelation(m *Module) string {
|
||||||
if m.Main {
|
if m.Main {
|
||||||
return "main"
|
return "main"
|
||||||
@ -313,11 +332,17 @@ func getRelation(m *Module) string {
|
|||||||
return "direct"
|
return "direct"
|
||||||
}
|
}
|
||||||
|
|
||||||
func getToolChains(f string) string {
|
func getToolChains(m *Module) string {
|
||||||
if f == "" {
|
var mf string
|
||||||
|
if m.GoMod != "" {
|
||||||
|
mf = m.GoMod
|
||||||
|
} else if m.Dir != "" {
|
||||||
|
mf = filepath.Join(m.Dir, "go.mod")
|
||||||
|
}
|
||||||
|
if mf == "" {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
file, err := GetModFile(f)
|
file, err := GetModFile(mf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -371,12 +396,6 @@ func UpdateList() {
|
|||||||
spin.Suffix = " Checking for updates..."
|
spin.Suffix = " Checking for updates..."
|
||||||
spin.Start()
|
spin.Start()
|
||||||
|
|
||||||
//modFile, err := GetModFile("go.mod")
|
|
||||||
//if err != nil {
|
|
||||||
// spin.Stop()
|
|
||||||
// logrus.Errorf("get go.mod file failed: %v", err)
|
|
||||||
// return
|
|
||||||
//}
|
|
||||||
modules, err := analyzed("go", "list", "-m", "-u", "-json", "-mod=readonly", "all")
|
modules, err := analyzed("go", "list", "-m", "-u", "-json", "-mod=readonly", "all")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
spin.Stop()
|
spin.Stop()
|
||||||
@ -400,12 +419,12 @@ func UpdateList() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(tableRows) == 0 {
|
if len(tableRows) == 0 {
|
||||||
awesome := color.New(color.FgHiGreen, color.Bold).Sprint("✔ Awesome!")
|
awesome := color.New(color.FgHiGreen, color.Bold).Sprint("✔ Empty!")
|
||||||
fmt.Printf(" %s All of your dependencies are up-to-date.\n", awesome)
|
fmt.Printf(" %s All of your dependencies are empty.\n", awesome)
|
||||||
} else {
|
} else {
|
||||||
table := tablewriter.NewWriter(os.Stdout)
|
table := tablewriter.NewWriter(os.Stdout)
|
||||||
table.SetHeader([]string{"Package", "Relation", "Current", "Latest"})
|
table.SetHeader([]string{"Package", "Relation", "Current", "Latest"})
|
||||||
table.SetBorder(false)
|
table.EnableBorder(true)
|
||||||
table.AppendBulk(tableRows)
|
table.AppendBulk(tableRows)
|
||||||
table.Render()
|
table.Render()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user