// ListBookmarks prints the list of bookmarks with their content func ListBookmarks(env *environments.Environment) { bookmarksFileInfos, err := env.GetBookmarksFileInfos() if err != nil { utils.Logger.Panic(err) } maxLength := 1 for _, fileInfo := range bookmarksFileInfos { name := fileInfo.Name() if len(name) > maxLength { maxLength = len(name) } } template := "%-" + strconv.Itoa(maxLength) + "s %s\n" utils.Logger.WithFields(logrus.Fields{ "template": template, }).Info("Calculated template to print") for _, fileInfo := range bookmarksFileInfos { fileName := fileInfo.Name() content, err := ioutil.ReadFile(env.GetBookmarkFileName(fileName)) if err != nil { utils.Logger.WithFields(logrus.Fields{ "filename": fileName, "error": err, }).Warn("Cannot read a content of the file so skip") continue } fmt.Printf(template, fileName, string(content)) } }
// ExecuteBookmark executes command by its bookmark name. func ExecuteBookmark(name string, interactive bool, pseudoTTY bool, env *environments.Environment) { content, err := ioutil.ReadFile(env.GetBookmarkFileName(name)) if err != nil { utils.Logger.Panic("Unknown bookmark") } execute(string(content), env.Shell, interactive, pseudoTTY) }
// Bookmark implements "b" (bookmark) command. func Bookmark(commandNumber int, bookmarkAs string, env *environments.Environment) { if commandNumber < 0 { utils.Logger.Panic("Command number should be >= 0") } commandsKeeper, err := historyentries.GetCommands(historyentries.GetCommandsAll, nil, env) if err != nil { utils.Logger.Panic(err) } commands := commandsKeeper.Result().([]historyentries.HistoryEntry) if len(commands) <= commandNumber { utils.Logger.Panic("Command number does not exist") } command := commands[commandNumber-1] filename := env.GetBookmarkFileName(bookmarkAs) file, err := os.Create(filename) if err != nil { utils.Logger.Panicf("Cannot create bookmark %s: %v", filename, err) } defer file.Close() file.WriteString(command.GetCommand()) }
// RemoveBookmarks removes the list of bookmarks from the storage. func RemoveBookmarks(bookmarks []string, env *environments.Environment) { for _, bookmark := range bookmarks { utils.RemoveWithLogging(env.GetBookmarkFileName(bookmark)) } }