Skip to content

Commit

Permalink
enhance type for platform for platform errorr (#9)
Browse files Browse the repository at this point in the history
* enhance type

* enhance index file type
  • Loading branch information
konoui authored Mar 28, 2021
1 parent fe73459 commit d74b9d5
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 25 deletions.
39 changes: 33 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

func initPlatform() string {
return "osx"
return tldr.PlatformOSX.String()
}

const (
Expand All @@ -37,7 +37,7 @@ const (
)

type config struct {
platform string
platform tldr.Platform
language string
update bool
confirm bool
Expand All @@ -49,11 +49,18 @@ type config struct {
// NewRootCmd create a new cmd for root
func NewRootCmd() *cobra.Command {
cfg := new(config)
var ptString string
rootCmd := &cobra.Command{
Use: "tldr <cmd>",
Short: "show cmd examples",
Args: cobra.MinimumNArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
if err := cfg.setPlatform(ptString); err != nil {
awf.SetEmptyWarning(strings.Title(err.Error()),
"supported are linux/osx/sunos/windows").
Output()
return nil
}
if err := cfg.initTldr(); err != nil {
return err
}
Expand All @@ -71,7 +78,8 @@ func NewRootCmd() *cobra.Command {
}
rootCmd.PersistentFlags().BoolVarP(&cfg.version, versionFlag, string(versionFlag[0]), false, "show the client version")
rootCmd.PersistentFlags().BoolVarP(&cfg.update, updateFlag, string(updateFlag[0]), false, "update tldr database")
rootCmd.PersistentFlags().StringVarP(&cfg.platform, platformFlag, string(platformFlag[0]), initPlatform(), "select from linux/osx/sunos/windows")
rootCmd.PersistentFlags().StringVarP(&ptString, platformFlag, string(platformFlag[0]),
initPlatform(), "select from linux/osx/sunos/windows")
rootCmd.PersistentFlags().StringVarP(&cfg.language, languageFlag, "L", "", "select language e.g.) en")

// internal flag
Expand Down Expand Up @@ -126,12 +134,30 @@ func makeUsageItem(p *pflag.Flag) *alfred.Item {
Valid(false)
}

func (cfg *config) setPlatform(ptString string) error {
platforms := []tldr.Platform{
tldr.PlatformCommon,
tldr.PlatformLinux,
tldr.PlatformOSX,
tldr.PlatformWindows,
tldr.PlatformSunos,
}
for _, pt := range platforms {
if ptString == pt.String() {
cfg.platform = pt
return nil
}
}
return fmt.Errorf("%s is unsupported platform", ptString)
}

func (cfg *config) initTldr() error {
base, err := alfred.GetDataDir()
if err != nil {
return err
}
path := filepath.Join(base, "data")

// Note turn off update option as we update database explicitly
opt := &tldr.Options{
Update: false,
Expand Down Expand Up @@ -226,17 +252,18 @@ func (cfg *config) printFuzzyPages(cmds []string) error {
suggestions := index.Commands.Search(cmds)
for _, cmd := range suggestions {
complete := cmd.Name
if cmd.Platform[0] != "common" {
// TODO when multi pt, computer pt must be used
if pt := cmd.Platforms[0]; pt != tldr.PlatformCommon && pt != cfg.platform {
complete = fmt.Sprintf("-%s %s %s",
string(platformFlag[0]),
cmd.Platform[0],
pt,
cmd.Name,
)
}
awf.Append(
alfred.NewItem().
Title(cmd.Name).
Subtitle(fmt.Sprintf("Platforms: %s", strings.Join(cmd.Platform, ","))).
Subtitle(fmt.Sprintf("Platforms: %s", fmt.Sprintf("%s", cmd.Platforms))).
Valid(false).
Autocomplete(complete).
Icon(
Expand Down
13 changes: 10 additions & 3 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestExecute(t *testing.T) {
{
name: "version flag is the highest priority",
args: args{
command: "-v tldr -p -a",
command: "-v tldr -L -a",
filepath: testdataPath("test_output_version.json"),
},
},
Expand Down Expand Up @@ -112,14 +112,14 @@ func TestExecute(t *testing.T) {
{
name: "string flag but no value and invalid flag",
args: args{
command: "-p -a",
command: "-L -a",
filepath: testdataPath("test_output_no-input.json"),
},
},
{
name: "string flag but no value",
args: args{
command: "--fuzzy -p",
command: "--fuzzy -L",
filepath: testdataPath("test_output_usage.json"),
},
},
Expand All @@ -137,6 +137,13 @@ func TestExecute(t *testing.T) {
filepath: testdataPath("test_output_usage.json"),
},
},
{
name: "invalid platform flag",
args: args{
command: "-p a",
filepath: testdataPath("test_output_platform-error.json"),
},
},
}

for _, tt := range tests {
Expand Down
14 changes: 7 additions & 7 deletions cmd/testdata/test_output_git-checkout_with_fuzzy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"items": [
{
"title": "git checkout",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand All @@ -11,7 +11,7 @@
},
{
"title": "git check attr",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand All @@ -20,7 +20,7 @@
},
{
"title": "git check ignore",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand All @@ -29,7 +29,7 @@
},
{
"title": "git check mailmap",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand All @@ -38,7 +38,7 @@
},
{
"title": "git checkout index",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand All @@ -47,7 +47,7 @@
},
{
"title": "git check ref format",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand All @@ -56,7 +56,7 @@
},
{
"title": "git cherry pick",
"subtitle": "Platforms: common",
"subtitle": "Platforms: [common]",
"icon": {
"path": "candidate.png"
},
Expand Down
12 changes: 12 additions & 0 deletions cmd/testdata/test_output_platform-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"items": [
{
"title": "A Is Unsupported Platform",
"subtitle": "supported are linux/osx/sunos/windows",
"icon": {
"path": "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
},
"valid": false
}
]
}
2 changes: 1 addition & 1 deletion cmd/testdata/test_output_pstree_with_fuzzy.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"items": [
{
"title": "pstree",
"subtitle": "Platforms: linux",
"subtitle": "Platforms: [linux]",
"icon": {
"path": "candidate.png"
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/tldr/fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

// CmdInfo contains name, platform, language
type CmdInfo struct {
Name string `json:"name"`
Platform []string `json:"platform"`
Language []string `json:"language"`
Name string `json:"name"`
Platforms []Platform `json:"platform"`
Languages []string `json:"language"`
}

// Cmds a slice of CmdInfo
Expand Down
22 changes: 18 additions & 4 deletions pkg/tldr/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,28 @@ import (
"github.com/pkg/errors"
)

type Platform string

const (
PlatformCommon Platform = "common"
PlatformWindows Platform = "windows"
PlatformLinux Platform = "linux"
PlatformSunos Platform = "sunos"
PlatformOSX Platform = "osx"
)

func (pt Platform) String() string {
return string(pt)
}

const (
pageSourceURL = "https://tldr.sh/assets/tldr.zip"
languageCodeEN = "en"
)

// Options are tldr functions
type Options struct {
Platform string
Platform Platform
Language string
Update bool
}
Expand All @@ -26,7 +40,7 @@ type Options struct {
type Tldr struct {
path string
pageSourceURL string
platforms []string
platforms []Platform
languages []string
update bool
}
Expand All @@ -42,7 +56,7 @@ func New(tldrPath string, opt *Options) *Tldr {
return &Tldr{
path: tldrPath,
pageSourceURL: pageSourceURL,
platforms: []string{opt.Platform, "common"},
platforms: []Platform{opt.Platform, PlatformCommon},
languages: getLanguages(opt.Language),
update: opt.Update,
}
Expand Down Expand Up @@ -93,7 +107,7 @@ func (t *Tldr) FindPage(cmds []string) (*Page, error) {
page := strings.Join(cmds, "-") + ".md"
for _, ptDir := range t.platforms {
for _, lang := range t.languages {
path := filepath.Join(t.path, getLangDir(lang), ptDir, page)
path := filepath.Join(t.path, getLangDir(lang), ptDir.String(), page)
if !pathExists(path) {
// if cmd does not exist, try to find it in next platform/language
continue
Expand Down
2 changes: 1 addition & 1 deletion pkg/tldr/pages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func TestOnInitialize(t *testing.T) {
tldr: Tldr{
path: "/.tldr",
pageSourceURL: pageSourceURL,
platforms: []string{"linux", "common"},
platforms: []Platform{PlatformLinux, PlatformCommon},
languages: []string{},
update: false,
},
Expand Down

0 comments on commit d74b9d5

Please sign in to comment.