github.com/mweagle/Sparta@v1.15.0/magefile/actions.go (about) 1 package spartamage 2 3 import ( 4 "errors" 5 "log" 6 "os" 7 "strconv" 8 9 "github.com/magefile/mage/mg" 10 "github.com/magefile/mage/sh" 11 ) 12 13 // Log is a mage verbose aware log function 14 func Log(formatSpecifier string, args ...interface{}) { 15 if mg.Verbose() { 16 if len(args) != 0 { 17 log.Printf(formatSpecifier, args...) 18 } else { 19 log.Print(formatSpecifier) 20 } 21 } 22 } 23 24 // Script is a 2d array of commands to run as a script 25 func Script(commands [][]string) error { 26 for _, eachCommand := range commands { 27 var commandErr error 28 if len(eachCommand) <= 1 { 29 commandErr = sh.Run(eachCommand[0]) 30 } else { 31 commandErr = sh.Run(eachCommand[0], eachCommand[1:]...) 32 } 33 if commandErr != nil { 34 return commandErr 35 } 36 } 37 return nil 38 } 39 40 // ApplyToSource is a mage compatible function that applies a 41 // command to your source tree 42 func ApplyToSource(fileExtension string, 43 ignoredGlobs []string, 44 commandParts ...string) error { 45 if len(commandParts) <= 0 { 46 return errors.New("applyToSource requires a command to apply to source files") 47 } 48 eligibleSourceFiles, eligibleSourceFilesErr := sourceFilesOfType(fileExtension, ignoredGlobs) 49 if eligibleSourceFilesErr != nil { 50 return eligibleSourceFilesErr 51 } 52 53 Log(header) 54 Log("Applying `%s` to %d `*.%s` source files", commandParts[0], len(eligibleSourceFiles), fileExtension) 55 Log(header) 56 57 commandArgs := []string{} 58 if len(commandParts) > 1 { 59 commandArgs = append(commandArgs, commandParts[1:]...) 60 } 61 for _, eachFile := range eligibleSourceFiles { 62 applyArgs := append(commandArgs, eachFile) 63 applyErr := sh.Run(commandParts[0], applyArgs...) 64 if applyErr != nil { 65 return applyErr 66 } 67 } 68 return nil 69 } 70 71 // SpartaCommand issues a go run command that encapsulates resolving 72 // global env vars that can be translated into Sparta command line options 73 func SpartaCommand(commandParts ...string) error { 74 noopValue := "" 75 parsedBool, parsedBoolErr := strconv.ParseBool(os.Getenv("NOOP")) 76 if parsedBoolErr == nil && parsedBool { 77 noopValue = "--noop" 78 } 79 curDir, curDirErr := os.Getwd() 80 if curDirErr != nil { 81 return errors.New("Failed to get current directory. Error: " + curDirErr.Error()) 82 } 83 setenvErr := os.Setenv(mg.VerboseEnv, "1") 84 if setenvErr != nil { 85 return setenvErr 86 } 87 commandArgs := []string{ 88 "run", 89 curDir, 90 } 91 commandArgs = append(commandArgs, commandParts...) 92 if noopValue != "" { 93 commandArgs = append(commandArgs, "--noop") 94 } 95 return sh.Run("go", 96 commandArgs...) 97 } 98 99 // Test runs the tests in verbose mode 100 func Test() error { 101 verboseFlag := "" 102 if mg.Verbose() { 103 verboseFlag = "-v" 104 } 105 return sh.Run("go", "test", verboseFlag, ".") 106 } 107 108 // Provision deploys the given service 109 func Provision() error { 110 // Get the bucketName 111 bucketName := os.Getenv("S3_BUCKET") 112 if bucketName == "" { 113 return errors.New("Provision requires env.S3_BUCKET to be defined") 114 } 115 return SpartaCommand("provision", "--s3Bucket", bucketName) 116 } 117 118 // Describe deploys the given service 119 func Describe() error { 120 // Get the bucketName 121 bucketName := os.Getenv("S3_BUCKET") 122 if bucketName == "" { 123 return errors.New("Describe requires env.S3_BUCKET to be defined") 124 } 125 return SpartaCommand("describe", "--s3Bucket", bucketName, "--out", "graph.html") 126 } 127 128 // Delete deletes the given service 129 func Delete() error { 130 return SpartaCommand("delete") 131 } 132 133 // Explore opens up the terminal GUI 134 func Explore() error { 135 // Get the bucketName 136 return SpartaCommand("explore") 137 } 138 139 // Status returns a report for the given status 140 func Status(plaintext ...bool) error { 141 if len(plaintext) == 1 && plaintext[0] { 142 return SpartaCommand("status") 143 } 144 return SpartaCommand("status", "--redact") 145 } 146 147 // Version returns version information about the service and embedded Sparta version 148 func Version() error { 149 return SpartaCommand("version") 150 }