Skip to content

Commit

Permalink
feature: start command - close #8
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed May 10, 2024
1 parent 6b3d6a8 commit a035c4a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
19 changes: 8 additions & 11 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,27 @@ func Init_Run_Command(parentCmd *cobra.Command, app *types.AppContext) {
Short: "Runs a command by name",
Long: `Runs a command by name which is defined in packages.ya(m)l file.`,
Run: func(cmd *cobra.Command, args []string) {
commandsToExecute := []string{}
scriptsToExecute := []string{}

for _, scriptName := range args {
scriptName = strings.TrimSpace(scriptName)
if scriptName == "" {
continue
}

cmdToExecute, ok := app.PackagesFile.Scripts[scriptName]
_, ok := app.PackagesFile.Scripts[scriptName]
if !ok {
utils.CloseWithError(fmt.Errorf("script '%v' not found", scriptName))
return
}

commandsToExecute = append(commandsToExecute, cmdToExecute)
scriptsToExecute = append(scriptsToExecute, scriptName)
}

for _, cmdToExecute := range commandsToExecute {
app.Debug(fmt.Sprintf("Running '%v' ...", cmdToExecute))

p := utils.CreateShellCommand(cmdToExecute)

if err := p.Run(); err != nil {
utils.CloseWithError(err)
if len(scriptsToExecute) == 0 {
app.RunCurrentProject()
} else {
for _, scriptName := range scriptsToExecute {
app.RunScript(scriptName)
}
}
},
Expand Down
52 changes: 52 additions & 0 deletions commands/start.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// MIT License
//
// Copyright (c) 2024 Marcel Joachim Kloubert (https://marcel.coffee)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package commands

import (
"github.com/mkloubert/go-package-manager/types"
"github.com/spf13/cobra"
)

const startScriptName = "start"

func Init_Start_Command(parentCmd *cobra.Command, app *types.AppContext) {
var startCmd = &cobra.Command{
Use: "start",
Aliases: []string{"s"},
Short: "Runs current project",
Long: `Runs the current project or 'start' script, if defined.`,
Run: func(cmd *cobra.Command, args []string) {
_, ok := app.PackagesFile.Scripts[startScriptName]

if ok {
app.RunScript(startScriptName, args...)
} else {
app.RunCurrentProject(args...)
}
},
}

parentCmd.AddCommand(
startCmd,
)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
// initialize commands
commands.Init_Install_Command(rootCmd, &app)
commands.Init_Run_Command(rootCmd, &app)
commands.Init_Start_Command(rootCmd, &app)
commands.Init_Tidy_Command(rootCmd, &app)

// execute
Expand Down
18 changes: 18 additions & 0 deletions types/app_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,21 @@ func (app *AppContext) GetModuleUrls(moduleNameOrUrl string) []string {

return urls
}

// app.RunCurrentProject() - runs the current go project
func (app *AppContext) RunCurrentProject(additionalArgs ...string) {
p := utils.CreateShellCommandByArgs("go", "run", ".")

app.Debug(fmt.Sprintf("Running '%v' ...", "go run ."))
utils.RunCommand(p, additionalArgs...)
}

// app.RunScript() - runs a script defined in packages.y(a)ml file
func (app *AppContext) RunScript(scriptName string, additionalArgs ...string) {
cmdToExecute := app.PackagesFile.Scripts[scriptName]

p := utils.CreateShellCommand(cmdToExecute)

app.Debug(fmt.Sprintf("Running script '%v' ...", scriptName))
utils.RunCommand(p, additionalArgs...)
}
9 changes: 9 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,12 @@ func GetBoolFlag(cmd *cobra.Command, name string, defaultValue bool) bool {

return defaultValue
}

// RunCommand() - runs a command and exists on error
func RunCommand(p *exec.Cmd, additionalArgs ...string) {
p.Args = append(p.Args, additionalArgs...)

if err := p.Run(); err != nil {
CloseWithError(err)
}
}

0 comments on commit a035c4a

Please sign in to comment.