2025-04-17 15:40:54 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2025-04-20 05:28:56 +00:00
|
|
|
"strings"
|
2025-04-17 15:40:54 +00:00
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// viewCmd represents the view command
|
|
|
|
var viewCmd = &cobra.Command{
|
|
|
|
Use: "view <name>",
|
|
|
|
Example: "mctl view test",
|
|
|
|
Short: "View saved commands",
|
2025-04-20 05:28:56 +00:00
|
|
|
Long: `Load command using the supplied name and displays it in the terminal, 'all' will list every saved command`,
|
2025-04-17 15:40:54 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2025-04-17 21:25:54 +00:00
|
|
|
var cm map[string]any
|
|
|
|
cmdMap := viper.Get("customcmd")
|
|
|
|
if cmdMap == nil {
|
|
|
|
fmt.Println("no custom commands found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cm = cmdMap.(map[string]any)
|
2025-04-20 05:28:56 +00:00
|
|
|
|
|
|
|
if strings.EqualFold(args[0], "all") {
|
|
|
|
//show all commands
|
|
|
|
fmt.Println("\nCommands: ")
|
|
|
|
for k, v := range cm {
|
|
|
|
fmt.Printf("%s - %s\n", k, v)
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-04-17 21:25:54 +00:00
|
|
|
custom, ok := cm[args[0]]
|
|
|
|
if !ok {
|
|
|
|
fmt.Println("command not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-04-17 17:32:16 -04:00
|
|
|
fmt.Printf("Command: %s\n", custom.(string))
|
2025-04-17 15:40:54 +00:00
|
|
|
},
|
|
|
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return errors.New("name argument is required")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(viewCmd)
|
|
|
|
}
|