Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add autosync before local change for new and edit commands #307

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ func edit(cmd *cobra.Command, args []string) (err error) {
editor := config.Conf.General.Editor
snippetFile := config.Conf.General.SnippetFile

// Sync snippets beforehand to get the latest version if the remote is newer
if config.Conf.Gist.AutoSync {
if err := petSync.AutoSync(snippetFile); err != nil {
return err
}
}

// file content before editing
before := fileContent(snippetFile)

Expand Down
18 changes: 14 additions & 4 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func new(cmd *cobra.Command, args []string) (err error) {

lineCount := countSnippetLines()

// Get the command from the user
if len(args) > 0 {
command = strings.Join(args, " ")
fmt.Fprintf(color.Output, "%s %s\n", color.HiYellowString("Command>"), command)
Expand Down Expand Up @@ -218,11 +219,12 @@ func new(cmd *cobra.Command, args []string) (err error) {
return err
}
}
// Get the description from the user
description, err = scan(color.HiGreenString("Description> "), os.Stdout, os.Stdin, false)
if err != nil {
return err
}

// Get the tags from the user
if config.Flag.Tag {
var t string
if t, err = scan(color.HiCyanString("Tag> "), os.Stdout, os.Stdin, true); err != nil {
Expand All @@ -234,12 +236,21 @@ func new(cmd *cobra.Command, args []string) (err error) {
}
}

snippetFile := config.Conf.General.SnippetFile

// Sync snippets beforehand to get the latest version if the remote is newer
if config.Conf.Gist.AutoSync {
if err := petSync.AutoSync(snippetFile); err != nil {
return err
}
}

for _, s := range snippets.Snippets {
if s.Description == description {
return fmt.Errorf("snippet [%s] already exists", description)
}
}

// Save the command, description, and tags as a new snippet
newSnippet := snippet.SnippetInfo{
Description: description,
Command: command,
Expand All @@ -249,8 +260,7 @@ func new(cmd *cobra.Command, args []string) (err error) {
if err = snippets.Save(); err != nil {
return err
}

snippetFile := config.Conf.General.SnippetFile
// Sync snippets after update to keep the remote up to date
if config.Conf.Gist.AutoSync {
return petSync.AutoSync(snippetFile)
}
Expand Down