Skip to content

Commit

Permalink
feat: remove command - close #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Kloubert committed May 11, 2024
1 parent d0603e3 commit 935272d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
13 changes: 12 additions & 1 deletion commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package commands

import (
"fmt"
"strings"

"github.com/mkloubert/go-package-manager/types"
Expand All @@ -31,6 +32,8 @@ import (
)

func init_add_alias_command(parentCmd *cobra.Command, app *types.AppContext) {
var reset bool

var addAliasCmd = &cobra.Command{
Use: "alias",
Aliases: []string{"a"},
Expand All @@ -40,11 +43,17 @@ func init_add_alias_command(parentCmd *cobra.Command, app *types.AppContext) {
Run: func(cmd *cobra.Command, args []string) {
alias := strings.TrimSpace(args[0])

if reset {
app.Debug(fmt.Sprintf("Resetting list of package alias '%v' ...", alias))
app.AliasesFile.Aliases[alias] = []string{}
}

sources := app.AliasesFile.Aliases[alias]

for _, s := range args[1:] {
s = strings.TrimSpace(s)
if s != "" {
app.Debug(fmt.Sprintf("Adding source '%v' for package alias '%v' ...", s, alias))
sources = append(sources, s)
}
}
Expand All @@ -58,6 +67,8 @@ func init_add_alias_command(parentCmd *cobra.Command, app *types.AppContext) {
},
}

addAliasCmd.Flags().BoolVarP(&reset, "reset", "r", false, "reset list before add")

parentCmd.AddCommand(
addAliasCmd,
)
Expand All @@ -66,7 +77,7 @@ func init_add_alias_command(parentCmd *cobra.Command, app *types.AppContext) {
func Init_Add_Command(parentCmd *cobra.Command, app *types.AppContext) {
var addCmd = &cobra.Command{
Use: "add [resource]",
Aliases: []string{"+"},
Aliases: []string{"ad"},
Short: "Add command",
Long: `Adds a resource.`,
Run: func(cmd *cobra.Command, args []string) {
Expand Down
76 changes: 76 additions & 0 deletions commands/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// 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 (
"fmt"
"strings"

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

func init_remove_alias_command(parentCmd *cobra.Command, app *types.AppContext) {
var removeAliasCmd = &cobra.Command{
Use: "alias [alias name]",
Aliases: []string{"a"},
Short: "Remove package alias",
Long: `Removes one or more aliases.`,
Run: func(cmd *cobra.Command, args []string) {
for _, a := range args {
alias := strings.TrimSpace(a)

app.Debug(fmt.Sprintf("Removing package alias '%v' ...", alias))
delete(app.AliasesFile.Aliases, alias)
}

err := app.UpdateAliasesFile()
if err != nil {
utils.CloseWithError(err)
}
},
}

parentCmd.AddCommand(
removeAliasCmd,
)
}

func Init_Remove_Command(parentCmd *cobra.Command, app *types.AppContext) {
var removeCmd = &cobra.Command{
Use: "remove [resource]",
Aliases: []string{"rm"},
Short: "Remove command",
Long: `Removes a resource.`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

init_remove_alias_command(removeCmd, app)

parentCmd.AddCommand(
removeCmd,
)
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func main() {
commands.Init_Install_Command(rootCmd, &app)
commands.Init_Pull_Command(rootCmd, &app)
commands.Init_Push_Command(rootCmd, &app)
commands.Init_Remove_Command(rootCmd, &app)
commands.Init_Run_Command(rootCmd, &app)
commands.Init_Start_Command(rootCmd, &app)
commands.Init_Sync_Command(rootCmd, &app)
Expand Down

0 comments on commit 935272d

Please sign in to comment.