-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: hide completion cmd Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com> * feat: from-json and to-json Signed-off-by: Carlos Alexandro Becker <caarlos0@gmail.com>
- Loading branch information
Showing
6 changed files
with
152 additions
and
1 deletion.
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,61 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
|
||
"github.com/caarlos0/tasktimer/internal/model" | ||
"github.com/caarlos0/tasktimer/internal/store" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type fromJSONCmd struct { | ||
cmd *cobra.Command | ||
} | ||
|
||
func newFromJSONCmd() *fromJSONCmd { | ||
var cmd = &cobra.Command{ | ||
Use: "from-json", | ||
Short: "Imports a JSON into a project - WARNING: it will wipe the project first, use with care!", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var project = cmd.Parent().Flag("project").Value.String() | ||
db, f, err := setup(project) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
defer f.Close() | ||
|
||
input, err := ioutil.ReadFile(args[0]) | ||
if err != nil { | ||
return fmt.Errorf("failed to read %s: %w", args[0], err) | ||
} | ||
|
||
var tasks []model.Task | ||
if err := json.Unmarshal(input, &tasks); err != nil { | ||
return fmt.Errorf("input json is not in the correct format: %w", err) | ||
} | ||
|
||
tmp, err := ioutil.TempFile("", "tasktimer-"+project) | ||
if err != nil { | ||
return fmt.Errorf("failed to create backup file: %w", err) | ||
} | ||
if _, err := db.Backup(tmp, 0); err != nil { | ||
return fmt.Errorf("failed to backup to %s: %w", tmp.Name(), err) | ||
} | ||
|
||
log.Printf("backup made to %s\n", tmp.Name()) | ||
|
||
if err := db.DropAll(); err != nil { | ||
return fmt.Errorf("failed to clear database: %w", err) | ||
} | ||
|
||
return store.LoadTasks(db, tasks) | ||
}, | ||
} | ||
|
||
return &fromJSONCmd{cmd: cmd} | ||
} |
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,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/caarlos0/tasktimer/internal/ui" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
type toJSONCmd struct { | ||
cmd *cobra.Command | ||
} | ||
|
||
func newToJSONCmd() *toJSONCmd { | ||
var cmd = &cobra.Command{ | ||
Use: "to-json", | ||
Short: "Exports the database as JSON", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
var project = cmd.Parent().Flag("project").Value.String() | ||
db, f, err := setup(project) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
defer f.Close() | ||
|
||
return ui.WriteProjectJSON(db, project, os.Stdout) | ||
}, | ||
} | ||
|
||
return &toJSONCmd{cmd: cmd} | ||
} |
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,27 @@ | ||
package ui | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/caarlos0/tasktimer/internal/store" | ||
"github.com/dgraph-io/badger/v3" | ||
) | ||
|
||
// WriteProjectJSON writes the project task list in JSON format to the given | ||
// io.Writer. | ||
func WriteProjectJSON(db *badger.DB, project string, w io.Writer) error { | ||
tasks, err := store.GetTaskList(db) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
bts, err := json.Marshal(tasks) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = w.Write(bts) | ||
|
||
return err | ||
} |