-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from kernle32dll/tagging
Implement tag adding and removal via commands
- Loading branch information
Showing
10 changed files
with
271 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/kernle32dll/ew/internal" | ||
|
||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// AddPathsCommand adds one or multiple paths to a tag. | ||
type AddPathsCommand struct { | ||
output io.Writer | ||
config internal.Config | ||
args []string | ||
} | ||
|
||
// NewAddPathsCommand creates a new AddPathsCommand. | ||
func NewAddPathsCommand( | ||
output io.Writer, | ||
config internal.Config, | ||
args []string, | ||
) *AddPathsCommand { | ||
return &AddPathsCommand{ | ||
output: output, | ||
config: config, | ||
args: args, | ||
} | ||
} | ||
|
||
func (c AddPathsCommand) Execute() error { | ||
if len(c.args) == 0 { | ||
return errors.New("missing @tag to add path to") | ||
} | ||
|
||
// Current path to tag | ||
if len(c.args) == 1 { | ||
return c.addCurrentPath() | ||
} | ||
|
||
// Multiple paths | ||
return c.addPaths() | ||
} | ||
|
||
func (c AddPathsCommand) addPaths() error { | ||
tag := c.args[len(c.args)-1] | ||
if !strings.HasPrefix(tag, "@") { | ||
return fmt.Errorf("expected @tag for last argument, got %q", tag) | ||
} | ||
tag = strings.TrimPrefix(tag, "@") | ||
|
||
paths := c.args[:1] | ||
|
||
c.config.AddPathsToTag(tag, paths...) | ||
|
||
if _, err := c.config.ReWriteConfig(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(c.output, "Added paths to tag @%s:\n", tag) | ||
for _, path := range paths { | ||
fmt.Fprintf(c.output, " %s\n", path) | ||
} | ||
return nil | ||
} | ||
|
||
func (c AddPathsCommand) addCurrentPath() error { | ||
tag := c.args[0] | ||
if !strings.HasPrefix(tag, "@") { | ||
return fmt.Errorf("expected @tag, got %q", tag) | ||
} | ||
tag = strings.TrimPrefix(tag, "@") | ||
|
||
path, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.config.AddPathsToTag(tag, path) | ||
|
||
if _, err := c.config.ReWriteConfig(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(c.output, "Added path %s to tag @%s\n", path, tag) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/kernle32dll/ew/internal" | ||
|
||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// RmPathsCommand removes one or multiple paths from a tag. | ||
type RmPathsCommand struct { | ||
output io.Writer | ||
config internal.Config | ||
args []string | ||
} | ||
|
||
// NewRmPathsCommand creates a new RmPathsCommand. | ||
func NewRmPathsCommand( | ||
output io.Writer, | ||
config internal.Config, | ||
args []string, | ||
) *RmPathsCommand { | ||
return &RmPathsCommand{ | ||
output: output, | ||
config: config, | ||
args: args, | ||
} | ||
} | ||
|
||
func (c RmPathsCommand) Execute() error { | ||
if len(c.args) == 0 { | ||
return errors.New("missing @tag to remove path from") | ||
} | ||
|
||
// Current path from tag | ||
if len(c.args) == 1 { | ||
return c.rmCurrentPath() | ||
} | ||
|
||
// Multiple paths | ||
return c.rmPaths() | ||
} | ||
|
||
func (c RmPathsCommand) rmPaths() error { | ||
tag := c.args[len(c.args)-1] | ||
if !strings.HasPrefix(tag, "@") { | ||
return fmt.Errorf("expected @tag for last argument, got %q", tag) | ||
} | ||
tag = strings.TrimPrefix(tag, "@") | ||
|
||
paths := c.args[:1] | ||
|
||
c.config.RemovePathsFromTag(tag, paths...) | ||
|
||
if _, err := c.config.ReWriteConfig(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(c.output, "Removed paths from tag @%s:\n", tag) | ||
for _, path := range paths { | ||
fmt.Fprintf(c.output, " %s\n", path) | ||
} | ||
return nil | ||
} | ||
|
||
func (c RmPathsCommand) rmCurrentPath() error { | ||
tag := c.args[0] | ||
if !strings.HasPrefix(tag, "@") { | ||
return fmt.Errorf("expected @tag, got %q", tag) | ||
} | ||
tag = strings.TrimPrefix(tag, "@") | ||
|
||
path, err := os.Getwd() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.config.RemovePathsFromTag(tag, path) | ||
|
||
if _, err := c.config.ReWriteConfig(); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(c.output, "Removing path %s from tag @%s\n", path, tag) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.