-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(list): support output formats (simple, command, yaml)
- Loading branch information
Showing
4 changed files
with
240 additions
and
8 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
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,93 @@ | ||
package cli | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/moyiz/na/internal/config" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type OutputFormat string | ||
|
||
const ( | ||
OutputFormatSimple = OutputFormat("simple") | ||
OutputFormatCmd = OutputFormat("cmd") | ||
OutputFormatYaml = OutputFormat("yaml") | ||
) | ||
|
||
func AllOutputFormats() []OutputFormat { | ||
return []OutputFormat{ | ||
OutputFormatSimple, | ||
OutputFormatCmd, | ||
OutputFormatYaml, | ||
} | ||
} | ||
|
||
func (o *OutputFormat) String() string { | ||
return string(*o) | ||
} | ||
|
||
func (o *OutputFormat) Set(v string) error { | ||
switch { | ||
case slices.Contains(AllOutputFormats(), OutputFormat(v)): | ||
*o = OutputFormat(v) | ||
return nil | ||
default: | ||
return errors.New("invalid format: " + v) | ||
} | ||
} | ||
|
||
func (o *OutputFormat) Type() string { | ||
return "format" | ||
} | ||
|
||
var OutputFormatToFunc = map[OutputFormat](func([]config.Alias) (string, error)){ | ||
OutputFormatSimple: simpleOutput, | ||
OutputFormatCmd: cmdOutput, | ||
OutputFormatYaml: yamlOutput, | ||
} | ||
|
||
func simpleOutput(aliases []config.Alias) (string, error) { | ||
builder := strings.Builder{} | ||
for _, alias := range aliases { | ||
builder.WriteString(fmt.Sprintln(alias.Name, "--", alias.Command)) | ||
} | ||
return builder.String(), nil | ||
} | ||
|
||
func cmdOutput(aliases []config.Alias) (string, error) { | ||
builder := strings.Builder{} | ||
for _, alias := range aliases { | ||
builder.WriteString(fmt.Sprintln("na add", alias.Name, "--", alias.Command)) | ||
} | ||
return builder.String(), nil | ||
} | ||
|
||
func yamlOutput(aliases []config.Alias) (string, error) { | ||
aliasesMap := make(map[string]interface{}) | ||
for _, alias := range aliases { | ||
aliasWalker := aliasesMap | ||
aliasParts := strings.Fields(alias.Name) | ||
n := len(aliasParts) | ||
for i := 0; i < n-1; i++ { | ||
if _, ok := aliasWalker[aliasParts[i]]; !ok { | ||
aliasWalker[aliasParts[i]] = make(map[string]interface{}) | ||
} | ||
aliasWalker = aliasWalker[aliasParts[i]].(map[string]interface{}) | ||
} | ||
aliasWalker[aliasParts[n-1]] = alias.Command | ||
} | ||
builder := strings.Builder{} | ||
if yamlData, err := yaml.Marshal(aliasesMap); err == nil { | ||
if yamlStr := string(yamlData); yamlStr != "{}\n" { | ||
builder.WriteString(fmt.Sprintln("---")) | ||
builder.WriteString(yamlStr) | ||
} | ||
} else { | ||
return string(yamlData), err | ||
} | ||
return builder.String(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/moyiz/na/internal/config" | ||
) | ||
|
||
func TestSimpleOutput(t *testing.T) { | ||
for _, testCase := range []struct { | ||
Aliases []config.Alias | ||
ExpectedOut string | ||
ExpectedErr error | ||
}{ | ||
{ | ||
Aliases: []config.Alias{ | ||
{ | ||
Name: "alias1", | ||
Command: "cmd1", | ||
}, | ||
{ | ||
Name: "alias2", | ||
Command: "cmd2", | ||
}, | ||
{ | ||
Name: "my alias1", | ||
Command: "my cmd1", | ||
}, | ||
{ | ||
Name: "my alias2", | ||
Command: "my cmd2", | ||
}, | ||
}, | ||
ExpectedOut: "alias1 -- cmd1\nalias2 -- cmd2\nmy alias1 -- my cmd1\nmy alias2 -- my cmd2\n", | ||
}, | ||
{ | ||
Aliases: []config.Alias{}, | ||
ExpectedOut: "", | ||
}, | ||
} { | ||
if out, err := simpleOutput(testCase.Aliases); out != testCase.ExpectedOut || err != testCase.ExpectedErr { | ||
t.Errorf("Simple output differ. Expected (%#v, %#v) but got (%#v, %#v)", testCase.ExpectedOut, testCase.ExpectedErr, out, err) | ||
} | ||
} | ||
} | ||
|
||
func TestCmdOutput(t *testing.T) { | ||
for _, testCase := range []struct { | ||
Aliases []config.Alias | ||
ExpectedOut string | ||
ExpectedErr error | ||
}{ | ||
{ | ||
Aliases: []config.Alias{ | ||
{ | ||
Name: "alias1", | ||
Command: "cmd1", | ||
}, | ||
{ | ||
Name: "alias2", | ||
Command: "cmd2", | ||
}, | ||
{ | ||
Name: "my alias1", | ||
Command: "my cmd1", | ||
}, | ||
{ | ||
Name: "my alias2", | ||
Command: "my cmd2", | ||
}, | ||
}, | ||
ExpectedOut: "na add alias1 -- cmd1\nna add alias2 -- cmd2\nna add my alias1 -- my cmd1\nna add my alias2 -- my cmd2\n", | ||
}, | ||
{ | ||
Aliases: []config.Alias{}, | ||
ExpectedOut: "", | ||
}, | ||
} { | ||
if out, err := cmdOutput(testCase.Aliases); out != testCase.ExpectedOut || err != testCase.ExpectedErr { | ||
t.Errorf("Command output differ. Expected (%#v, %#v) but got (%#v, %#v)", testCase.ExpectedOut, testCase.ExpectedErr, out, err) | ||
} | ||
} | ||
} | ||
|
||
func TestYamlOutput(t *testing.T) { | ||
for _, testCase := range []struct { | ||
Aliases []config.Alias | ||
ExpectedOut string | ||
ExpectedErr error | ||
}{ | ||
{ | ||
Aliases: []config.Alias{}, | ||
ExpectedOut: "", | ||
}, | ||
{ | ||
Aliases: []config.Alias{ | ||
{ | ||
Name: "alias1", | ||
Command: "cmd1", | ||
}, | ||
}, | ||
ExpectedOut: "---\nalias1: cmd1\n", | ||
}, | ||
{ | ||
Aliases: []config.Alias{ | ||
{ | ||
Name: "my alias1", | ||
Command: "my cmd1", | ||
}, | ||
{ | ||
Name: "my alias2", | ||
Command: "my cmd2", | ||
}, | ||
}, | ||
ExpectedOut: "---\nmy:\n alias1: my cmd1\n alias2: my cmd2\n", | ||
}, | ||
} { | ||
if out, err := yamlOutput(testCase.Aliases); out != testCase.ExpectedOut || err != testCase.ExpectedErr { | ||
t.Errorf("Command output differ. Expected (%#v, %#v) but got (%#v, %#v)", testCase.ExpectedOut, testCase.ExpectedErr, out, err) | ||
} | ||
} | ||
} |