58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
/*
|
|
Copyright © 2025 Jake jake.young.dev@gmail.com
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"code.jakeyoungdev.com/jake/mctl/database"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// viewCmd represents the view command
|
|
var viewCmd = &cobra.Command{
|
|
Use: "view <name>",
|
|
Example: "mctl view test",
|
|
Short: "View saved commands",
|
|
Long: `Load command using the supplied name and displays it in the terminal, 'all' will list every saved command`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
db, err := database.New()
|
|
cobra.CheckErr(err)
|
|
defer db.Close()
|
|
if strings.EqualFold(args[0], "all") {
|
|
cmds, err := db.GetAllCmds()
|
|
cobra.CheckErr(err)
|
|
|
|
fmt.Println("\nCommands: ")
|
|
for _, c := range cmds {
|
|
fmt.Printf("%s - %s\n", c.Name, c.Command)
|
|
}
|
|
fmt.Println()
|
|
} else {
|
|
cmds, err := db.GetCmd(args[0])
|
|
cobra.CheckErr(err)
|
|
|
|
if cmds == "" {
|
|
fmt.Println("Command not found")
|
|
return
|
|
}
|
|
|
|
fmt.Printf("Command: %s\n", cmds)
|
|
}
|
|
},
|
|
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)
|
|
}
|