management

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddCmd = &cobra.Command{
	Use:     "add",
	Aliases: []string{"a"},
	Short:   "Add/Update a shell script snippet",
	Run: func(cmd *cobra.Command, args []string) {
		if AddCmdOpts.FromFile {

			cmd_impel.AddFromFiles(args, AddCmdOpts.SetEnable, AddCmdOpts.SetNameAsFileName)
		} else {
			// Add from Editor
			// Name Ask name
			// Content form Editor result
			// Enable set as AddAsSetEnable
			var realname string = AddCmdOpts.Name
			var realenable bool = AddCmdOpts.SetEnable
			if AddCmdOpts.UpdateMode {
				allrs, err := cmd_impel.GetAllSnippets()
				if err != nil {
					log.Errorf("Get all Snippets Error: %v", err)
				}
				if realname == "" {
					res := Ask.ChoiceOneOfSlice(Ask.ConvertToChoiceAbleSlice(allrs)).(defines.ShellSnippet)
					realname = res.Name
					realenable = res.IsEnable
				} else {
					found := false
					for _, rs := range allrs {
						if rs.Name == AddCmdOpts.Name {
							realname = rs.Name
							realenable = rs.IsEnable
							found = true
							break
						}
					}
					if !found {
						log.Errorf("User input name %v not found in database", AddCmdOpts.Name)
						os.Exit(-1)
					}
				}
			} else {
				if realname == "" {
					realname = Ask.ForName("Input snippet name")
				}
			}
			cmd_impel.AddFromEditor(realname, realenable)
		}
	},
}
View Source
var AddCmdOpts struct {
	FromFile          bool
	SetNameAsFileName bool
	SetEnable         bool
	UpdateMode        bool
	Name              string
}
View Source
var BackupCmd = &cobra.Command{
	Use:   "backup",
	Short: "Backup or recover snippets",
	Run: func(cmd *cobra.Command, args []string) {
		realbackupPath := BackupRecoverCmdOpts.BackupPath
		if BackupRecoverCmdOpts.RecoverMode {

			if BackupRecoverCmdOpts.BackupPath == "" {
				backupFp := Ask.ForName("Where is the backup path?")
				if !File.PathExists(backupFp) {
					log.Errorf("Can not found %s", backupFp)
					return
				}
				realbackupPath = backupFp
			}
			Database.Close()
			err := File.Copy(realbackupPath, Database.DBLocation)
			if err != nil {
				log.Errorf("Copy file error: %v", err)
				return
			}
			log.Infof("Recover DB from %s to %s success ", realbackupPath, Database.DBLocation)
		} else {

			if BackupRecoverCmdOpts.BackupPath == "" {
				backupFp := Ask.ForName("Where is the backup path?")
				if File.PathExists(backupFp) {
					log.Errorf("Backup path %s is exists", backupFp)
					return
				}
				realbackupPath = backupFp
			}
			err := File.Copy(Database.DBLocation, realbackupPath)
			if err != nil {
				log.Errorf("Copy file error: %v", err)
				return
			}
			log.Infof("Backup DB from %s to %s success ", Database.DBLocation, realbackupPath)
		}
	},
}
View Source
var BackupRecoverCmdOpts struct {
	BackupPath  string
	RecoverMode bool
}
View Source
var DelCmd = &cobra.Command{
	Use:     "del",
	Aliases: []string{"d", "rm", "remove", "r"},
	Short:   "Delete shell script snippets",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {
			cmd_impel.DeleteSnippetsFromNames(args)
		} else {
			cmd_impel.DeleteSnippetsFromSelect()
		}
		log.Infof("delete successful")
	},
}
View Source
var DisableCmd = &cobra.Command{
	Use:     "disable",
	Aliases: []string{"dis"},
	Short:   "disable shell script snippets",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {
			cmd_impel.DisableSnippetsFromNames(args)
		} else {
			cmd_impel.DisableSnippetsFromSelect()
		}
		log.Infof("disabled successful")
	},
}
View Source
var EditCmd = &cobra.Command{
	Use:     "edit",
	Aliases: []string{"e"},
	Short:   "Edit/Update a shell script snippet",
	Run: func(cmd *cobra.Command, args []string) {
		// Edit from Editor
		// Name Ask name
		// Content form Editor result
		// Enable set as EditAsSetEnable
		var realname string = EditCmdOpts.Name
		var realenable bool = EditCmdOpts.SetEnable
		allrs, err := cmd_impel.GetAllSnippets()
		if err != nil {
			log.Errorf("Get all Snippets Error: %v", err)
		}
		if realname == "" {
			res := Ask.ChoiceOneOfSlice(Ask.ConvertToChoiceAbleSlice(allrs)).(defines.ShellSnippet)
			realname = res.Name
			realenable = res.IsEnable
		} else {
			found := false
			for _, rs := range allrs {
				if rs.Name == EditCmdOpts.Name {
					realname = rs.Name
					realenable = rs.IsEnable
					found = true
					break
				}
			}
			if !found {
				log.Errorf("User input name %v not found in database", EditCmdOpts.Name)
				os.Exit(-1)
			}
		}
		cmd_impel.AddFromEditor(realname, realenable)
	},
}
View Source
var EditCmdOpts struct {
	SetNameAsFileName bool
	SetEnable         bool
	UpdateMode        bool
	Name              string
}
View Source
var EnableCmd = &cobra.Command{
	Use:     "enable",
	Aliases: []string{"e"},
	Short:   "enable shell script snippets",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) > 0 {
			cmd_impel.EnableSnippetsFromNames(args)
		} else {
			cmd_impel.EnableSnippetsFromSelect()
		}
		log.Infof("enabled successful")
	},
}
View Source
var ListCmd = &cobra.Command{
	Use:     "list",
	Aliases: []string{"ls", "l"},
	Short:   "List and print snippets summary as a table",
	Run: func(cmd *cobra.Command, args []string) {
		cmd_impel.ListAndPrint()
	},
}
View Source
var ShareCmd = &cobra.Command{
	Use:     "share",
	Aliases: []string{"s"},
	Short:   "Share or print snippets",
	Run: func(cmd *cobra.Command, args []string) {
		var target defines.ShellSnippet
		if ShareCmdOpts.Name == "" {
			target = cmd_impel.SelectSnippet()
		} else {
			t, err := cmd_impel.FoundSnippet(ShareCmdOpts.Name)
			if err != nil {
				log.Errorf("Get Share Snippet error: %v", err)
				os.Exit(-1)
			}
			target = t
		}
		if ShareCmdOpts.DryRun || ShareCmdOpts.Platform == "" {
			cmd_impel.PrintTarget(target)
		} else {
			if ShareCmdOpts.PlatformSecret == "not set" {
				log.Errorf("Platform secret not set")
				os.Exit(-1)
			}
			var (
				e error
				u string = ""
			)

			switch ShareCmdOpts.Platform {
			case "github":
				u, e = cmd_impel.ShareOnGithub(target, ShareCmdOpts.PlatformSecret)
			case "pastebin":
				u, e = cmd_impel.ShareOnPastebin(target, ShareCmdOpts.PlatformSecret)
			default:
				e = errors.New(fmt.Sprintf("Platform %s not support", ShareCmdOpts.Platform))
			}
			if e != nil {
				log.Errorf("Share Error: %v", e)
			} else {
				log.Infof("Share success on url: %v", u)
			}
		}
	},
}
View Source
var ShareCmdOpts struct {
	Name           string
	DryRun         bool
	Platform       string
	PlatformSecret string
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL