Skip to content

Commit

Permalink
Refactored cmd line API in order to use "mite-go <entity> <action>"
Browse files Browse the repository at this point in the history
  • Loading branch information
phiros committed Apr 2, 2019
1 parent 64bc3bd commit 9558caf
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 94 deletions.
52 changes: 52 additions & 0 deletions cmd-entries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"os"
"strings"
"time"
)

func init() {
entriesCommand.AddCommand(entriesListCommand)
rootCmd.AddCommand(entriesCommand)
}

var entriesCommand = &cobra.Command{
Use: "entries",
Short: "lists & adds time entries",
Run: entriesListCommand.Run,
}

var entriesListCommand = &cobra.Command{
Use: "list",
Short: "list time entries",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
to := time.Now()
from := to.AddDate(0, 0, -7)
direction := mite.DirectionAsc

entries, err := api.TimeEntries(&mite.TimeEntryParameters{
To: &to,
From: &from,
Direction: &direction,
})
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

t := tabby.New()
t.AddHeader("id", "notes", "date", "time", "project,service")
for _, entry := range entries {
trimmedNotes := strings.Replace(entry.Note, "\r\n", ",", -1)
shortendNotes := fmt.Sprintf("%.50s", trimmedNotes)
shortenedProjectService := fmt.Sprintf("%.50s", entry.ProjectName+","+entry.ServiceName)
t.AddLine(entry.Id, shortendNotes, entry.Date, entry.Duration.String(), shortenedProjectService)
}
t.Print()
},
}
94 changes: 0 additions & 94 deletions cmd-list.go

This file was deleted.

39 changes: 39 additions & 0 deletions cmd-projects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"os"
)

func init() {
projectsCommand.AddCommand(listProjectsCommand)
rootCmd.AddCommand(projectsCommand)
}

var projectsCommand = &cobra.Command{
Use: "projects",
Short: "list & adds projects",
Run: listProjectsCommand.Run,
}

var listProjectsCommand = &cobra.Command{
Use: "list",
Short: "list projects",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
projects, err := api.Projects()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

t := tabby.New()
t.AddHeader("id", "name", "notes")
for _, project := range projects {
t.AddLine(project.Id, project.Name, project.Note)
}
t.Print()
},
}
39 changes: 39 additions & 0 deletions list-services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"github.com/cheynewallace/tabby"
"github.com/leanovate/mite-go/mite"
"github.com/spf13/cobra"
"os"
)

func init() {
servicesCommand.AddCommand(listServicesCommand)
rootCmd.AddCommand(servicesCommand)
}

var servicesCommand = &cobra.Command{
Use: "services",
Short: "list & adds services",
Run: listServicesCommand.Run,
}

var listServicesCommand = &cobra.Command{
Use: "list",
Short: "list services",
Run: func(cmd *cobra.Command, args []string) {
api := mite.NewMiteApi(configGetApiUrl(), configGetApiKey())
services, err := api.Services()
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
}

t := tabby.New()
t.AddHeader("id", "name", "notes")
for _, service := range services {
t.AddLine(service.Id, service.Name, service.Note)
}
t.Print()
},
}

0 comments on commit 9558caf

Please sign in to comment.