Skip to content

Commit

Permalink
feat: list projects
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
  • Loading branch information
caarlos0 committed Feb 5, 2021
1 parent 941799b commit 195c494
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
65 changes: 65 additions & 0 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/charmbracelet/glamour"
"github.com/mattn/go-isatty"
gap "github.com/muesli/go-app-paths"
"github.com/spf13/cobra"
)

type listCmd struct {
cmd *cobra.Command
}

func newListCmd() *listCmd {
cmd := &cobra.Command{
Use: "list",
Short: "List all projects",
RunE: func(cmd *cobra.Command, args []string) error {
home := gap.NewScope(gap.User, "tasktimer")
datas, err := home.DataDirs()
if err != nil {
return err
}

var buf bytes.Buffer
for _, data := range datas {
if _, err := os.Stat(data); err != nil && os.IsNotExist(err) {
continue
}
if err := filepath.Walk(data, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if filepath.Ext(path) == ".db" {
_, _ = fmt.Fprintln(&buf, "- "+strings.Replace(filepath.Base(path), ".db", "", 1))
return filepath.SkipDir
}
return nil
}); err != nil {
return err
}
}

if isatty.IsTerminal(os.Stdout.Fd()) {
rendered, err := glamour.RenderWithEnvironmentConfig(buf.String())
if err != nil {
return err
}
fmt.Print(rendered)
return nil
}

fmt.Print(buf.String())
return nil
},
}

return &listCmd{cmd: cmd}
}
9 changes: 8 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ func newRootCmd(version string, exit func(int)) *rootCmd {

cmd.PersistentFlags().StringVarP(&root.project, "project", "p", "default", "Project name")

cmd.AddCommand(newRerportCmd().cmd, newCompletionCmd().cmd, newPathsCmd().cmd, newToJSONCmd().cmd, newFromJSONCmd().cmd)
cmd.AddCommand(
newRerportCmd().cmd,
newCompletionCmd().cmd,
newPathsCmd().cmd,
newToJSONCmd().cmd,
newFromJSONCmd().cmd,
newListCmd().cmd,
)

root.cmd = cmd
return root
Expand Down

0 comments on commit 195c494

Please sign in to comment.