Skip to content

Commit f6049f1

Browse files
Code improvements
1 parent 20588d6 commit f6049f1

6 files changed

Lines changed: 235 additions & 256 deletions

File tree

cmd/dependencies.go

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
package cmd
22

33
import (
4-
"github.com/hashload/boss/core"
4+
"os"
5+
"path/filepath"
6+
7+
"github.com/hashload/boss/consts"
8+
"github.com/hashload/boss/core/installer"
9+
"github.com/hashload/boss/env"
10+
"github.com/hashload/boss/models"
11+
"github.com/hashload/boss/msg"
12+
"github.com/hashload/boss/utils"
13+
"github.com/masterminds/semver"
514
"github.com/spf13/cobra"
15+
"github.com/xlab/treeprint"
16+
)
17+
18+
const (
19+
updated = 0
20+
outdated = 1
21+
usingMaster = 2
622
)
723

824
var showVersion bool
25+
var tree = treeprint.New()
926

1027
var dependenciesCmd = &cobra.Command{
1128
Use: "dependencies",
@@ -24,11 +41,88 @@ var dependenciesCmd = &cobra.Command{
2441
List package dependencies with version control:
2542
boss dependencies <pkg> --version`,
2643
Run: func(cmd *cobra.Command, args []string) {
27-
core.PrintDependencies(showVersion)
44+
printDependencies(showVersion)
2845
},
2946
}
3047

3148
func init() {
3249
RootCmd.AddCommand(dependenciesCmd)
3350
dependenciesCmd.Flags().BoolVarP(&showVersion, "version", "v", false, "show dependency version")
3451
}
52+
53+
func printDependencies(showVersion bool) {
54+
pkg, err := models.LoadPackage(false)
55+
if err != nil {
56+
if os.IsNotExist(err) {
57+
msg.Die("boss.json not exists in " + env.GetCurrentDir())
58+
} else {
59+
msg.Die("Fail on open dependencies file: %s", err)
60+
}
61+
}
62+
63+
master := tree.AddBranch(pkg.Name + ":")
64+
deps := pkg.GetParsedDependencies()
65+
printDeps(nil, deps, pkg.Lock, master, showVersion)
66+
print(tree.String())
67+
}
68+
69+
func printDeps(dep *models.Dependency, deps []models.Dependency, lock models.PackageLock, tree treeprint.Tree, showVersion bool) {
70+
var localTree treeprint.Tree
71+
72+
if dep != nil {
73+
localTree = printSingleDependency(dep, lock, tree, showVersion)
74+
} else {
75+
localTree = tree
76+
}
77+
78+
for _, dep := range deps {
79+
pkgModule, err := models.LoadPackageOther(filepath.Join(env.GetModulesDir(), dep.GetName(), consts.FilePackage))
80+
if err != nil {
81+
printSingleDependency(&dep, lock, localTree, showVersion)
82+
} else {
83+
deps := pkgModule.GetParsedDependencies()
84+
printDeps(&dep, deps, lock, localTree, showVersion)
85+
}
86+
}
87+
}
88+
89+
func printSingleDependency(dep *models.Dependency, lock models.PackageLock, tree treeprint.Tree, showVersion bool) treeprint.Tree {
90+
var output = dep.GetName()
91+
92+
if showVersion {
93+
output += "@"
94+
output += lock.GetInstalled(*dep).Version
95+
}
96+
97+
switch isOutdated(*dep, lock.GetInstalled(*dep).Version) {
98+
case outdated:
99+
output += " outdated"
100+
break
101+
case usingMaster:
102+
output += " using master"
103+
break
104+
}
105+
106+
return tree.AddBranch(output)
107+
}
108+
109+
func isOutdated(dependency models.Dependency, version string) int {
110+
installer.GetDependency(dependency)
111+
info, err := models.RepoData(dependency.GetHashName())
112+
if err != nil {
113+
utils.HandleError(err)
114+
} else {
115+
locked, err := semver.NewVersion(version)
116+
if err != nil {
117+
return usingMaster
118+
}
119+
constraint, _ := semver.NewConstraint(dependency.GetVersion())
120+
for _, value := range info.Versions {
121+
version, err := semver.NewVersion(value)
122+
if err == nil && version.GreaterThan(locked) && constraint.Check(version) {
123+
return outdated
124+
}
125+
}
126+
}
127+
return updated
128+
}

cmd/init.go

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
package cmd
22

33
import (
4-
"github.com/hashload/boss/core"
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"regexp"
9+
"strings"
10+
11+
"github.com/hashload/boss/env"
12+
"github.com/hashload/boss/models"
513
"github.com/spf13/cobra"
614
)
715

@@ -17,11 +25,69 @@ var initCmd = &cobra.Command{
1725
Initialize a new project without having it ask any questions:
1826
boss init --quiet`,
1927
Run: func(cmd *cobra.Command, args []string) {
20-
core.InitializeBossPackage(quiet)
28+
doInitialization(quiet)
2129
},
2230
}
2331

2432
func init() {
2533
RootCmd.AddCommand(initCmd)
2634
initCmd.Flags().BoolVarP(&quiet, "quiet", "q", false, "without asking questions")
2735
}
36+
37+
func doInitialization(quiet bool) {
38+
if !quiet {
39+
printHead()
40+
}
41+
42+
pkgJson, _ := models.LoadPackage(true)
43+
44+
var folderName = ""
45+
rxp, err := regexp.Compile(`^.+\` + string(filepath.Separator) + `([^\\]+)$`)
46+
if err == nil {
47+
allString := rxp.FindAllStringSubmatch(env.GetCurrentDir(), -1)
48+
folderName = allString[0][1]
49+
}
50+
51+
if quiet {
52+
pkgJson.Name = folderName
53+
pkgJson.Version = "1.0.0"
54+
pkgJson.MainSrc = "./"
55+
} else {
56+
pkgJson.Name = getParamOrDef("Package name ("+folderName+")", folderName)
57+
pkgJson.Homepage = getParamOrDef("Homepage", "")
58+
pkgJson.Version = getParamOrDef("Version (1.0.0)", "1.0.0")
59+
pkgJson.Description = getParamOrDef("Description", "")
60+
pkgJson.MainSrc = getParamOrDef("Source folder (./src)", "./src")
61+
}
62+
63+
json := pkgJson.Save()
64+
fmt.Println("\n" + string([]byte(json)))
65+
}
66+
67+
func getParamOrDef(msg string, def string) string {
68+
fmt.Print(msg + ": ")
69+
rr := bufio.NewReader(os.Stdin)
70+
if res, e := rr.ReadString('\n'); e == nil && res != "\n" {
71+
res = strings.ReplaceAll(res, "\t", "")
72+
res = strings.ReplaceAll(res, "\n", "")
73+
res = strings.ReplaceAll(res, "\r", "")
74+
if res == "" {
75+
return def
76+
} else {
77+
return res
78+
}
79+
}
80+
return def
81+
}
82+
83+
func printHead() {
84+
println(`
85+
This utility will walk you through creating a boss.json file.
86+
It only covers the most common items, and tries to guess sensible defaults.
87+
88+
Use 'boss install <pkg>' afterwards to install a package and
89+
save it as a dependency in the boss.json file.
90+
91+
Press ^C at any time to quit.
92+
`)
93+
}

cmd/login.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package cmd
22

33
import (
4-
"github.com/hashload/boss/core"
4+
"fmt"
5+
"log"
6+
"os/user"
7+
"path/filepath"
8+
"syscall"
9+
10+
"github.com/hashload/boss/env"
11+
"github.com/hashload/boss/msg"
512
"github.com/spf13/cobra"
13+
"golang.org/x/crypto/ssh/terminal"
614
)
715

816
var removeLogin bool
@@ -14,7 +22,7 @@ var loginCmd = &cobra.Command{
1422
boss login <repo>`,
1523
Aliases: []string{"adduser", "add-user"},
1624
Run: func(cmd *cobra.Command, args []string) {
17-
core.Login(removeLogin, args)
25+
login(removeLogin, args)
1826
},
1927
}
2028

@@ -23,3 +31,64 @@ func init() {
2331
loginCmd.Flags().BoolVarP(&removeLogin, "rm", "r", false, "remove login")
2432
RootCmd.AddCommand(loginCmd)
2533
}
34+
35+
func login(removeLogin bool, args []string) {
36+
configuration := env.GlobalConfiguration
37+
38+
if removeLogin {
39+
delete(configuration.Auth, args[0])
40+
configuration.SaveConfiguration()
41+
return
42+
}
43+
44+
var auth *env.Auth
45+
var repo string
46+
if len(args) > 0 && args[0] != "" {
47+
repo = args[0]
48+
auth = configuration.Auth[args[0]]
49+
} else {
50+
repo = getParamOrDef("Url to login (ex: github.com)", "")
51+
if repo == "" {
52+
msg.Die("Empty is not valid!!")
53+
}
54+
auth = configuration.Auth[repo]
55+
}
56+
57+
if auth == nil {
58+
auth = &env.Auth{}
59+
}
60+
61+
auth.UseSsh = getParamBoolean("Use SSH")
62+
if auth.UseSsh {
63+
auth.Path = getParamOrDef("Path of ssh private key("+getSshKeyPath()+")", getSshKeyPath())
64+
} else {
65+
auth.SetUser(getParamOrDef("Username", ""))
66+
auth.SetPass(getPass("Password"))
67+
}
68+
configuration.Auth[repo] = auth
69+
configuration.SaveConfiguration()
70+
71+
}
72+
73+
func getPass(description string) string {
74+
fmt.Print(description + ": ")
75+
76+
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
77+
if err != nil {
78+
msg.Die("Error on get pass: %s", err)
79+
}
80+
return string(bytePassword)
81+
}
82+
83+
func getSshKeyPath() string {
84+
usr, e := user.Current()
85+
if e != nil {
86+
log.Fatal(e)
87+
}
88+
return filepath.Join(usr.HomeDir, ".ssh", "id_rsa")
89+
}
90+
91+
func getParamBoolean(msgS string) bool {
92+
msg.Print(msgS + "(y or n): ")
93+
return msg.PromptUntilYorN()
94+
}

0 commit comments

Comments
 (0)