From 145a4c36caf960fc9a43b16c105df997e819a04d Mon Sep 17 00:00:00 2001 From: Arctic Ice Studio Date: Sat, 13 Jul 2019 20:36:45 +0200 Subject: [PATCH] `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 --- cmd/snowsaw/bootstrap/bootstrap.go | 131 +++++++++++++++++++++++++++++ cmd/snowsaw/snowsaw.go | 1 + 2 files changed, 132 insertions(+) create mode 100644 cmd/snowsaw/bootstrap/bootstrap.go diff --git a/cmd/snowsaw/bootstrap/bootstrap.go b/cmd/snowsaw/bootstrap/bootstrap.go new file mode 100644 index 0000000..faf137d --- /dev/null +++ b/cmd/snowsaw/bootstrap/bootstrap.go @@ -0,0 +1,131 @@ +// Copyright (C) 2017-present Arctic Ice Studio +// Copyright (C) 2017-present Sven Greb +// +// Project: snowsaw +// Repository: https://github.com/arcticicestudio/snowsaw +// License: MIT + +// Author: Arctic Ice Studio +// Author: Sven Greb +// Since: 0.4.0 + +// Package bootstrap provides the command to run the main task of processing all configured snowblocks. +package bootstrap + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" + + "github.com/fatih/color" + "github.com/spf13/cobra" + + "github.com/arcticicestudio/snowsaw/pkg/config" + "github.com/arcticicestudio/snowsaw/pkg/prt" + "github.com/arcticicestudio/snowsaw/pkg/snowblock" + "github.com/arcticicestudio/snowsaw/pkg/util/filesystem" +) + +type cmdOptions struct { + SnowblockPaths []string +} + +// NewBootstrapCmd creates and configures a new `bootstrap` command. +func NewBootstrapCmd() *cobra.Command { + o := cmdOptions{} + bootstrapCmd := &cobra.Command{ + Use: "bootstrap", + Short: "Bootstraps all configured snowblocks", + Run: func(cmd *cobra.Command, args []string) { + o.prepare(cmd, args) + o.run(cmd, args) + }, + } + + bootstrapCmd.Flags().StringSliceVarP( + &o.SnowblockPaths, "snowblocks", "s", []string{}, "comma-separated paths to individual snowblock directories") + + return bootstrapCmd +} + +func (o *cmdOptions) prepare(cmd *cobra.Command, args []string) { + // Use explicit snowblocks if specified, otherwise find all snowblocks within the base directories. + if len(o.SnowblockPaths) > 0 { + prt.Debugf("Using individual snowblocks instead of configured base directories(s): %s", + color.CyanString("%v", o.SnowblockPaths)) + config.AppConfig.Snowblocks.Paths = o.SnowblockPaths + } else { + if err := o.readSnowblockDirectories(); err != nil { + prt.Errorf("Failed to read snowblocks from base directories: %v", err) + os.Exit(1) + } + o.SnowblockPaths = config.AppConfig.Snowblocks.Paths + } +} + +func (o *cmdOptions) readSnowblockDirectories() error { + var validBaseDirs []string + for _, baseDir := range config.AppConfig.Snowblocks.BaseDirs { + expBaseDir, expBaseDirErr := filesystem.ExpandPath(baseDir) + if expBaseDirErr != nil { + return fmt.Errorf("could not expand base snowblock directory path: %v", expBaseDirErr) + } + baseDirExists, baseDirExistsChkErr := filesystem.DirExists(expBaseDir) + if baseDirExistsChkErr != nil { + return fmt.Errorf("could not read snowblock base directory: %v", baseDirExistsChkErr) + } + if baseDirExists { + sbDirs, sbDirListErr := ioutil.ReadDir(expBaseDir) + if sbDirListErr != nil { + return fmt.Errorf("could not read snowblock base directory: %s", color.RedString("%v", sbDirListErr)) + } + for _, sbFileInfo := range sbDirs { + if sbFileInfo.IsDir() { + config.AppConfig.Snowblocks.Paths = append( + config.AppConfig.Snowblocks.Paths, filepath.Join(expBaseDir, sbFileInfo.Name())) + } + } + validBaseDirs = append(validBaseDirs, baseDir) + continue + } + prt.Warnf("Ignoring non-existent snowblock base directory: %s", color.CyanString(baseDir)) + } + + if len(validBaseDirs) > 0 { + prt.Debugf("Processing configured snowblock base directories: %s", color.CyanString("%v", validBaseDirs)) + } + return nil +} + +func (o *cmdOptions) run(cmd *cobra.Command, args []string) { + for _, path := range o.SnowblockPaths { + sb := snowblock.NewSnowblock(path) + err := sb.Validate(config.SnowblockTaskRunnerRegistry.GetAll()) + if err != nil { + prt.Errorf("Failed to validate snowblock %s: %v", + color.CyanString(filepath.Base(path)), color.RedString(err.Error())) + os.Exit(1) + } + if !sb.IsValid { + prt.Warnf("Skipped processing of invalid snowblock %s", color.CyanString(filepath.Base(sb.Path))) + continue + } + + err = sb.Dispatch() + if err != nil { + prt.Errorf("Failed to process snowblock %s: %v", + color.CyanString(filepath.Base(path)), color.RedString(err.Error())) + os.Exit(1) + } + if sb.IsValid { + prt.Successf("Successfully bootstrapped snowblock %s", color.CyanString(filepath.Base(path))) + } + } + + if len(o.SnowblockPaths) > 0 { + prt.Successf("Bootstrapped all configured snowblocks") + } else { + prt.Warnf("No valid snowblocks found") + } +} diff --git a/cmd/snowsaw/snowsaw.go b/cmd/snowsaw/snowsaw.go index 91ae003..2c0f494 100644 --- a/cmd/snowsaw/snowsaw.go +++ b/cmd/snowsaw/snowsaw.go @@ -19,6 +19,7 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" + "github.com/arcticicestudio/snowsaw/cmd/snowsaw/bootstrap" "github.com/arcticicestudio/snowsaw/cmd/snowsaw/info" "github.com/arcticicestudio/snowsaw/pkg/config" "github.com/arcticicestudio/snowsaw/pkg/config/builder"