Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit cc674cb

Browse files
committed
Improvements
1 parent 4d7173d commit cc674cb

File tree

12 files changed

+139
-114
lines changed

12 files changed

+139
-114
lines changed

cmd/docker-setup/describe.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

78
"gopkg.in/yaml.v3"
89

910
"github.com/spf13/cobra"
10-
11-
"github.com/nicholasdille/docker-setup/pkg/tool"
1211
)
1312

1413
var describeOutput string
@@ -20,17 +19,15 @@ func initDescribeCmd() {
2019
}
2120

2221
var describeCmd = &cobra.Command{
23-
Use: "describe",
24-
Aliases: []string{"d", "info"},
25-
Short: "Show detailed information about tools",
26-
Long: header + "\nShow detailed information about tools",
27-
Args: cobra.ExactArgs(1),
28-
RunE: func(cmd *cobra.Command, args []string) error {
22+
Use: "describe",
23+
Aliases: []string{"d", "info"},
24+
Short: "Show detailed information about tools",
25+
Long: header + "\nShow detailed information about tools",
26+
Args: cobra.ExactArgs(1),
27+
ValidArgs: tools.GetNames(),
28+
RunE: func(cmd *cobra.Command, args []string) error {
2929
assertMetadataFileExists()
30-
tools, err := tool.LoadFromFile(metadataFile)
31-
if err != nil {
32-
return fmt.Errorf("Failed to load metadata from file %s: %s\n", metadataFile, err)
33-
}
30+
assertMetadataIsLoaded()
3431

3532
tool, err := tools.GetByName(args[0])
3633
if err != nil {
@@ -42,12 +39,20 @@ var describeCmd = &cobra.Command{
4239
tool.Print()
4340

4441
} else if describeOutput == "json" {
45-
data, _ := json.Marshal(tool)
42+
data, err := json.Marshal(tool)
43+
if err != nil {
44+
return fmt.Errorf("Failed to marshal to json: %s\n", err)
45+
}
4646
fmt.Println(string(data))
4747

4848
} else if describeOutput == "yaml" {
49-
data, _ := yaml.Marshal(tool)
50-
fmt.Println(string(data))
49+
yamlEncoder := yaml.NewEncoder(os.Stdout)
50+
yamlEncoder.SetIndent(2)
51+
defer yamlEncoder.Close()
52+
err := yamlEncoder.Encode(tool)
53+
if err != nil {
54+
return fmt.Errorf("Failed to encode yaml: %s\n", err)
55+
}
5156
}
5257

5358
return nil

cmd/docker-setup/init.go

+26
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ func directoryExists(directory string) bool {
3737
return err == nil
3838
}
3939

40+
func fileExists(file string) bool {
41+
log.Tracef("Checking if file %s exists", file)
42+
_, err := os.Stat(file)
43+
return err == nil
44+
}
45+
4046
func directoryIsWritable(directory string) bool {
4147
log.Tracef("Checking if directory %s is writable", directory)
4248
return unix.Access(directory, unix.W_OK) == nil
@@ -84,6 +90,22 @@ func assertMetadataFileExists() {
8490
}
8591
}
8692

93+
func loadMetadata() {
94+
var err error
95+
tools, err = tool.LoadFromFile(metadataFile)
96+
if err != nil {
97+
fmt.Printf("Failed to load metadata from file %s: %s\n", metadataFile, err)
98+
os.Exit(1)
99+
}
100+
}
101+
102+
func assertMetadataIsLoaded() {
103+
if len(tools.Tools) == 0 {
104+
fmt.Printf("Metadata is not loaded\n")
105+
os.Exit(1)
106+
}
107+
}
108+
87109
func initDockerSetup() {
88110
if alt_arch == "amd64" {
89111
arch = "x86_64"
@@ -95,4 +117,8 @@ func initDockerSetup() {
95117
log.Errorf("Unsupported architecture: %s", arch)
96118
os.Exit(1)
97119
}
120+
121+
if fileExists(metadataFile) {
122+
loadMetadata()
123+
}
98124
}

cmd/docker-setup/inspect.go

+7-11
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ import (
66
"github.com/spf13/cobra"
77
//log "github.com/sirupsen/logrus"
88
//"github.com/fatih/color"
9-
10-
"github.com/nicholasdille/docker-setup/pkg/tool"
119
)
1210

1311
func initInspectCmd() {
1412
rootCmd.AddCommand(inspectCmd)
1513
}
1614

1715
var inspectCmd = &cobra.Command{
18-
Use: "inspect",
19-
Short: "Inspect tool",
20-
Long: header + "\nInspect tools",
21-
Args: cobra.ExactArgs(1),
22-
RunE: func(cmd *cobra.Command, args []string) error {
16+
Use: "inspect",
17+
Short: "Inspect tool",
18+
Long: header + "\nInspect tools",
19+
Args: cobra.ExactArgs(1),
20+
ValidArgs: tools.GetNames(),
21+
RunE: func(cmd *cobra.Command, args []string) error {
2322
assertMetadataFileExists()
24-
tools, err := tool.LoadFromFile(metadataFile)
25-
if err != nil {
26-
return fmt.Errorf("Failed to load metadata from file %s: %s\n", metadataFile, err)
27-
}
23+
assertMetadataIsLoaded()
2824

2925
tool, err := tools.GetByName(args[0])
3026
if err != nil {

cmd/docker-setup/install.go

+53-35
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ var tagsMode bool
1616
var installedMode bool
1717
var check bool
1818
var plan bool
19-
var toolStatus map[string]tool.ToolStatus = make(map[string]tool.ToolStatus)
2019
var requestedTools tool.Tools
2120
var plannedTools tool.Tools
2221
var reinstall bool
@@ -28,11 +27,11 @@ func initInstallCmd() {
2827
rootCmd.AddCommand(installCmd)
2928

3029
installCmd.Flags().StringVarP(&installMode, "mode", "m", "default", "How to install (default, tags, installed)")
31-
installCmd.Flags().BoolVarP( &defaultMode, "default", "d", false, "Install default tools")
32-
installCmd.Flags().BoolVarP( &tagsMode, "tags", "t", false, "Install tool(s) matching tag")
30+
installCmd.Flags().BoolVarP( &defaultMode, "default", "", false, "Install default tools")
31+
installCmd.Flags().BoolVarP( &tagsMode, "tags", "", false, "Install tool(s) matching tag")
3332
installCmd.Flags().BoolVarP( &installedMode, "installed", "i", false, "Update installed tool(s)")
34-
installCmd.Flags().BoolVarP( &plan, "plan", "p", false, "Show tool(s) planned installation")
35-
installCmd.Flags().BoolVarP( &check, "check", "c", false, "Abort after checking versions")
33+
installCmd.Flags().BoolVarP( &plan, "plan", "", false, "Show tool(s) planned installation")
34+
installCmd.Flags().BoolVarP( &check, "check", "c", false, "Abort after checking versions")
3635
installCmd.Flags().BoolVarP( &reinstall, "reinstall", "r", false, "Reinstall tool(s)")
3736
installCmd.MarkFlagsMutuallyExclusive("mode", "default", "tags", "installed")
3837
installCmd.MarkFlagsMutuallyExclusive("check", "plan")
@@ -43,8 +42,8 @@ var installCmd = &cobra.Command{
4342
Aliases: []string{"i"},
4443
Short: "Install tools",
4544
Long: header + "\nInstall and update tools",
46-
ValidArgs: tools.GetNames(),
4745
Args: cobra.OnlyValidArgs,
46+
ValidArgs: tools.GetNames(),
4847
RunE: func(cmd *cobra.Command, args []string) error {
4948

5049
// Validation checks
@@ -56,31 +55,7 @@ var installCmd = &cobra.Command{
5655
}
5756

5857
assertMetadataFileExists()
59-
tools, err := tool.LoadFromFile(metadataFile)
60-
if err != nil {
61-
return fmt.Errorf("Failed to load metadata from file %s: %s\n", metadataFile, err)
62-
}
63-
64-
// Fill default values and replace variables
65-
for index, tool := range tools.Tools {
66-
log.Tracef("Getting status for requested tool %s", tool.Name)
67-
tools.Tools[index].ReplaceVariables(prefix + "/" + target, arch, alt_arch)
68-
69-
err := tools.Tools[index].GetBinaryStatus()
70-
if err != nil {
71-
return fmt.Errorf("Unable to determine binary status of %s: %s", tool.Name, err)
72-
}
73-
74-
err = tools.Tools[index].GetMarkerFileStatus(cacheDirectory)
75-
if err != nil {
76-
return fmt.Errorf("Unable to determine marker file status of %s: %s", tool.Name, err)
77-
}
78-
79-
err = tools.Tools[index].GetVersionStatus()
80-
if err != nil {
81-
return fmt.Errorf("Unable to determine version status of %s: %s", tool.Name, err)
82-
}
83-
}
58+
assertMetadataIsLoaded()
8459

8560
if defaultMode {
8661
installMode = "default"
@@ -92,6 +67,8 @@ var installCmd = &cobra.Command{
9267
installMode = "only-installed"
9368
}
9469

70+
log.Debugf("Using install mode %s", installMode)
71+
9572
// Collect requested tools based on mode
9673
if len(args) > 0 && installMode == "default" {
9774
installMode = "list"
@@ -106,8 +83,23 @@ var installCmd = &cobra.Command{
10683
requestedTools = tools.GetByTags([]string{"category/default"})
10784

10885
} else if installMode == "only-installed" {
109-
for _, tool := range tools.Tools {
110-
if toolStatus[tool.Name].BinaryPresent {
86+
log.Debugf("Collecting installed tools")
87+
for index, tool := range tools.Tools {
88+
log.Tracef("Getting status for requested tool %s", tool.Name)
89+
tools.Tools[index].ReplaceVariables(prefix + "/" + target, arch, alt_arch)
90+
91+
err := tools.Tools[index].GetBinaryStatus()
92+
if err != nil {
93+
return fmt.Errorf("Unable to determine binary status of %s: %s", tool.Name, err)
94+
}
95+
96+
err = tools.Tools[index].GetMarkerFileStatus(cacheDirectory)
97+
if err != nil {
98+
return fmt.Errorf("Unable to determine marker file status of %s: %s", tool.Name, err)
99+
}
100+
101+
if tools.Tools[index].Status.MarkerFilePresent && tools.Tools[index].Status.BinaryPresent {
102+
log.Tracef("Adding %s to requested tools", tool.Name)
111103
requestedTools.Tools = append(requestedTools.Tools, tool)
112104
}
113105
}
@@ -124,13 +116,39 @@ var installCmd = &cobra.Command{
124116
}
125117
log.Debugf("Planned %d tool(s)", len(plannedTools.Tools))
126118

119+
// Populate status of planned tools
120+
// TODO: Display spinner
121+
for index, tool := range plannedTools.Tools {
122+
log.Tracef("Getting status for requested tool %s", tool.Name)
123+
plannedTools.Tools[index].ReplaceVariables(prefix + "/" + target, arch, alt_arch)
124+
125+
err := plannedTools.Tools[index].GetBinaryStatus()
126+
if err != nil {
127+
return fmt.Errorf("Unable to determine binary status of %s: %s", tool.Name, err)
128+
}
129+
130+
err = plannedTools.Tools[index].GetMarkerFileStatus(cacheDirectory)
131+
if err != nil {
132+
return fmt.Errorf("Unable to determine marker file status of %s: %s", tool.Name, err)
133+
}
134+
135+
if plannedTools.Tools[index].Status.BinaryPresent && plannedTools.Tools[index].Status.MarkerFilePresent {
136+
// TODO: Run version check in parallel
137+
err := plannedTools.Tools[index].GetVersionStatus()
138+
if err != nil {
139+
return fmt.Errorf("Unable to determine version status of %s: %s", tool.Name, err)
140+
}
141+
}
142+
}
143+
127144
// Terminate if checking or planning
128145
if plan || check {
129-
plannedTools.ListWithStatus(toolStatus)
146+
// TODO: Improve output
147+
plannedTools.ListWithStatus()
130148
}
131149
if check {
132150
for _, tool := range plannedTools.Tools {
133-
if ! toolStatus[tool.Name].BinaryPresent || ! toolStatus[tool.Name].VersionMatches {
151+
if ! tool.Status.BinaryPresent || ! tool.Status.VersionMatches {
134152
return fmt.Errorf("Found missing or outdated tool")
135153
}
136154
}

cmd/docker-setup/list.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package main
22

33
import (
4-
"fmt"
5-
64
"github.com/spf13/cobra"
7-
8-
"github.com/nicholasdille/docker-setup/pkg/tool"
95
)
106

117
func initListCmd() {
@@ -20,10 +16,7 @@ var listCmd = &cobra.Command{
2016
Args: cobra.NoArgs,
2117
RunE: func(cmd *cobra.Command, args []string) error {
2218
assertMetadataFileExists()
23-
tools, err := tool.LoadFromFile(metadataFileName)
24-
if err != nil {
25-
return fmt.Errorf("Failed to load metadata from file %s: %s\n", metadataFileName, err)
26-
}
19+
assertMetadataIsLoaded()
2720

2821
tools.List()
2922

cmd/docker-setup/main.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ var header string = `
1818
|_|
1919
`
2020
var logLevel string
21-
//var no_color bool
2221

2322
var (
2423
rootCmd = &cobra.Command{
25-
Use: "docker-setup",
26-
Version: version,
27-
Short: header + "The container tools installer and updater",
24+
Use: "docker-setup",
25+
Version: version,
26+
Short: header + "The container tools installer and updater",
27+
SilenceUsage: true,
2828
}
2929
)
3030

@@ -62,11 +62,12 @@ func main() {
6262
return nil
6363
}
6464
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", log.WarnLevel.String(), "Log level (trace, debug, info, warning, error)")
65+
// TODO: Add flags --trace and --debug (make mutually exclusive)
6566
rootCmd.PersistentFlags().StringVarP(&prefix, "prefix", "p", "/", "Prefix for installation")
6667
rootCmd.PersistentFlags().StringVarP(&target, "target", "t", "usr/local", "Target directory for installation")
6768
rootCmd.PersistentFlags().StringVarP(&cacheDirectory, "cache-directory", "C", "/var/cache/docker-setup", "Cache directory")
6869
rootCmd.PersistentFlags().StringVarP(&libDirectory, "lib-directory", "L", "/var/lib/docker-setup", "Library directory")
69-
rootCmd.PersistentFlags().StringVarP(&metadataFileName, "metadata-file", "m", "metadata.json", "Metadata file")
70+
rootCmd.PersistentFlags().StringVarP(&metadataFileName, "metadata-file", "f", "metadata.json", "Metadata file")
7071

7172
rootCmd.Execute()
7273
}

cmd/docker-setup/search.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"fmt"
55

66
"github.com/spf13/cobra"
7-
8-
"github.com/nicholasdille/docker-setup/pkg/tool"
97
)
108

119
func initSearchCmd() {
@@ -20,17 +18,15 @@ func initSearchCmd() {
2018
}
2119

2220
var searchCmd = &cobra.Command{
23-
Use: "search <term>",
24-
Aliases: []string{"s"},
25-
Short: "Search for tools",
26-
Long: header + "\nSearch for tools",
27-
Args: cobra.ExactArgs(1),
28-
RunE: func(cmd *cobra.Command, args []string) error {
21+
Use: "search <term>",
22+
Aliases: []string{"s"},
23+
Short: "Search for tools",
24+
Long: header + "\nSearch for tools",
25+
Args: cobra.ExactArgs(1),
26+
ValidArgs: tools.GetNames(),
27+
RunE: func(cmd *cobra.Command, args []string) error {
2928
assertMetadataFileExists()
30-
tools, err := tool.LoadFromFile(metadataFileName)
31-
if err != nil {
32-
return fmt.Errorf("Failed to load metadata from file %s: %s\n", metadataFileName, err)
33-
}
29+
assertMetadataIsLoaded()
3430

3531
onlySearchInName, err := cmd.Flags().GetBool("only-names")
3632
if err != nil {

0 commit comments

Comments
 (0)