Skip to content

Commit 145a4c3

Browse files
bootstrap base command
Implemented the base command `bootstrap` to run the main task of processing all configured snowblocks. It uses the implementations of the snowblock API v0. Epic GH-33 Resolves GH-73
1 parent efdff96 commit 145a4c3

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

cmd/snowsaw/bootstrap/bootstrap.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright (C) 2017-present Arctic Ice Studio <[email protected]>
2+
// Copyright (C) 2017-present Sven Greb <[email protected]>
3+
//
4+
// Project: snowsaw
5+
// Repository: https://github.com/arcticicestudio/snowsaw
6+
// License: MIT
7+
8+
// Author: Arctic Ice Studio <[email protected]>
9+
// Author: Sven Greb <[email protected]>
10+
// Since: 0.4.0
11+
12+
// Package bootstrap provides the command to run the main task of processing all configured snowblocks.
13+
package bootstrap
14+
15+
import (
16+
"fmt"
17+
"io/ioutil"
18+
"os"
19+
"path/filepath"
20+
21+
"github.com/fatih/color"
22+
"github.com/spf13/cobra"
23+
24+
"github.com/arcticicestudio/snowsaw/pkg/config"
25+
"github.com/arcticicestudio/snowsaw/pkg/prt"
26+
"github.com/arcticicestudio/snowsaw/pkg/snowblock"
27+
"github.com/arcticicestudio/snowsaw/pkg/util/filesystem"
28+
)
29+
30+
type cmdOptions struct {
31+
SnowblockPaths []string
32+
}
33+
34+
// NewBootstrapCmd creates and configures a new `bootstrap` command.
35+
func NewBootstrapCmd() *cobra.Command {
36+
o := cmdOptions{}
37+
bootstrapCmd := &cobra.Command{
38+
Use: "bootstrap",
39+
Short: "Bootstraps all configured snowblocks",
40+
Run: func(cmd *cobra.Command, args []string) {
41+
o.prepare(cmd, args)
42+
o.run(cmd, args)
43+
},
44+
}
45+
46+
bootstrapCmd.Flags().StringSliceVarP(
47+
&o.SnowblockPaths, "snowblocks", "s", []string{}, "comma-separated paths to individual snowblock directories")
48+
49+
return bootstrapCmd
50+
}
51+
52+
func (o *cmdOptions) prepare(cmd *cobra.Command, args []string) {
53+
// Use explicit snowblocks if specified, otherwise find all snowblocks within the base directories.
54+
if len(o.SnowblockPaths) > 0 {
55+
prt.Debugf("Using individual snowblocks instead of configured base directories(s): %s",
56+
color.CyanString("%v", o.SnowblockPaths))
57+
config.AppConfig.Snowblocks.Paths = o.SnowblockPaths
58+
} else {
59+
if err := o.readSnowblockDirectories(); err != nil {
60+
prt.Errorf("Failed to read snowblocks from base directories: %v", err)
61+
os.Exit(1)
62+
}
63+
o.SnowblockPaths = config.AppConfig.Snowblocks.Paths
64+
}
65+
}
66+
67+
func (o *cmdOptions) readSnowblockDirectories() error {
68+
var validBaseDirs []string
69+
for _, baseDir := range config.AppConfig.Snowblocks.BaseDirs {
70+
expBaseDir, expBaseDirErr := filesystem.ExpandPath(baseDir)
71+
if expBaseDirErr != nil {
72+
return fmt.Errorf("could not expand base snowblock directory path: %v", expBaseDirErr)
73+
}
74+
baseDirExists, baseDirExistsChkErr := filesystem.DirExists(expBaseDir)
75+
if baseDirExistsChkErr != nil {
76+
return fmt.Errorf("could not read snowblock base directory: %v", baseDirExistsChkErr)
77+
}
78+
if baseDirExists {
79+
sbDirs, sbDirListErr := ioutil.ReadDir(expBaseDir)
80+
if sbDirListErr != nil {
81+
return fmt.Errorf("could not read snowblock base directory: %s", color.RedString("%v", sbDirListErr))
82+
}
83+
for _, sbFileInfo := range sbDirs {
84+
if sbFileInfo.IsDir() {
85+
config.AppConfig.Snowblocks.Paths = append(
86+
config.AppConfig.Snowblocks.Paths, filepath.Join(expBaseDir, sbFileInfo.Name()))
87+
}
88+
}
89+
validBaseDirs = append(validBaseDirs, baseDir)
90+
continue
91+
}
92+
prt.Warnf("Ignoring non-existent snowblock base directory: %s", color.CyanString(baseDir))
93+
}
94+
95+
if len(validBaseDirs) > 0 {
96+
prt.Debugf("Processing configured snowblock base directories: %s", color.CyanString("%v", validBaseDirs))
97+
}
98+
return nil
99+
}
100+
101+
func (o *cmdOptions) run(cmd *cobra.Command, args []string) {
102+
for _, path := range o.SnowblockPaths {
103+
sb := snowblock.NewSnowblock(path)
104+
err := sb.Validate(config.SnowblockTaskRunnerRegistry.GetAll())
105+
if err != nil {
106+
prt.Errorf("Failed to validate snowblock %s: %v",
107+
color.CyanString(filepath.Base(path)), color.RedString(err.Error()))
108+
os.Exit(1)
109+
}
110+
if !sb.IsValid {
111+
prt.Warnf("Skipped processing of invalid snowblock %s", color.CyanString(filepath.Base(sb.Path)))
112+
continue
113+
}
114+
115+
err = sb.Dispatch()
116+
if err != nil {
117+
prt.Errorf("Failed to process snowblock %s: %v",
118+
color.CyanString(filepath.Base(path)), color.RedString(err.Error()))
119+
os.Exit(1)
120+
}
121+
if sb.IsValid {
122+
prt.Successf("Successfully bootstrapped snowblock %s", color.CyanString(filepath.Base(path)))
123+
}
124+
}
125+
126+
if len(o.SnowblockPaths) > 0 {
127+
prt.Successf("Bootstrapped all configured snowblocks")
128+
} else {
129+
prt.Warnf("No valid snowblocks found")
130+
}
131+
}

cmd/snowsaw/snowsaw.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/fatih/color"
2020
"github.com/spf13/cobra"
2121

22+
"github.com/arcticicestudio/snowsaw/cmd/snowsaw/bootstrap"
2223
"github.com/arcticicestudio/snowsaw/cmd/snowsaw/info"
2324
"github.com/arcticicestudio/snowsaw/pkg/config"
2425
"github.com/arcticicestudio/snowsaw/pkg/config/builder"

0 commit comments

Comments
 (0)