✨ Move CLI commands to cli/ package for extensibility
- Rename internal/cmd/ to cli/ - Export commands: RootCmd, DropCmd, ConfigCmd - Enable custom app creation by importing cli package
This commit is contained in:
parent
5c8e2fc607
commit
8c23375208
4 changed files with 22 additions and 41 deletions
67
cli/config.go
Normal file
67
cli/config.go
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
Copyright © 2024 Dan Jones <danjones@goodevilgenius.org>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
fp "path/filepath"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var ConfigCmd = &cobra.Command{
|
||||
Use: "config",
|
||||
Short: "Save default config to file",
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
||||
print, _ := cmd.Flags().GetBool("print")
|
||||
if print {
|
||||
fmt.Fprintln(cmd.OutOrStdout(), config.ConfigPath)
|
||||
return nil
|
||||
}
|
||||
force, _ := cmd.Flags().GetBool("force")
|
||||
if !force {
|
||||
_, err = os.Stat(config.ConfigPath)
|
||||
if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("%s already exists. Use -f to overwrite", config.ConfigPath)
|
||||
}
|
||||
}
|
||||
dir := fp.Dir(config.ConfigPath)
|
||||
err = os.MkdirAll(dir, 0755)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Create(config.ConfigPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
c := config.DefaultStr()
|
||||
fmt.Fprint(f, c)
|
||||
return
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(ConfigCmd)
|
||||
|
||||
ConfigCmd.Flags().BoolP("force", "f", false, "Force overwrite")
|
||||
ConfigCmd.Flags().BoolP("print", "p", false, "Print path only")
|
||||
}
|
||||
128
cli/drop.go
Normal file
128
cli/drop.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
Copyright © 2024 Dan Jones <danjones@goodevilgenius.org>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cli
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"codeberg.org/danjones000/my-log/files"
|
||||
"codeberg.org/danjones000/my-log/formatters"
|
||||
"codeberg.org/danjones000/my-log/models"
|
||||
"codeberg.org/danjones000/my-log/tools"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var d Date
|
||||
var fields map[string]string
|
||||
var j Json
|
||||
var outJson bool
|
||||
|
||||
// DropCmd represents the drop command
|
||||
var DropCmd = &cobra.Command{
|
||||
Use: "drop log title",
|
||||
Short: "Add a new log entry",
|
||||
// Long: ``,
|
||||
Args: cobra.ExactArgs(2),
|
||||
SilenceUsage: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if outJson {
|
||||
config.Overrides["output.stdout.config.format"] = "json"
|
||||
}
|
||||
|
||||
log := args[0]
|
||||
title := args[1]
|
||||
ms := &models.Metas{}
|
||||
if len(j.RawMessage) > 8 {
|
||||
err := json.Unmarshal([]byte(j.RawMessage), ms)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for k, v := range fields {
|
||||
ms.AppendTo(k, tools.ParseString(v))
|
||||
}
|
||||
e := models.Entry{title, d.t, *ms}
|
||||
l := models.Log{log, []models.Entry{e}}
|
||||
err := files.Append(l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
form, err := formatters.Preferred()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
out, err := form.Log(l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(out) > 0 && out[len(out)-1] != 10 {
|
||||
out = append(out, 10)
|
||||
}
|
||||
fmt.Fprintf(cmd.OutOrStdout(), "%s", out)
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(DropCmd)
|
||||
|
||||
(&d).Set("now")
|
||||
DropCmd.Flags().VarP(&d, "date", "d", "Date for log entry")
|
||||
DropCmd.Flags().StringToStringVarP(&fields, "fields", "f", nil, "Fields you add to entry")
|
||||
DropCmd.Flags().VarP(&j, "json", "j", "Entire entry as json")
|
||||
DropCmd.Flags().BoolVarP(&outJson, "output_json", "o", false, "Output result as JSON")
|
||||
}
|
||||
|
||||
type Json struct {
|
||||
json.RawMessage
|
||||
}
|
||||
|
||||
func (j *Json) String() string {
|
||||
return string(j.RawMessage)
|
||||
}
|
||||
|
||||
func (j *Json) Set(in string) error {
|
||||
return json.Unmarshal([]byte(in), &j.RawMessage)
|
||||
}
|
||||
|
||||
func (j *Json) Type() string {
|
||||
return "json"
|
||||
}
|
||||
|
||||
type Date struct {
|
||||
s string
|
||||
t time.Time
|
||||
}
|
||||
|
||||
func (d *Date) String() string {
|
||||
return d.s
|
||||
}
|
||||
|
||||
func (d *Date) Set(in string) (err error) {
|
||||
d.s = in
|
||||
d.t, err = tools.ParseDate(in)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Date) Type() string {
|
||||
return "datetime"
|
||||
}
|
||||
41
cli/root.go
Normal file
41
cli/root.go
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
Copyright © 2024 Dan Jones <danjones@goodevilgenius.org>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package cli
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"codeberg.org/danjones000/my-log/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "my-log",
|
||||
Short: "A brief description of your application",
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
err := RootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
RootCmd.PersistentFlags().StringVarP(&config.ConfigPath, "config", "c", config.ConfigPath, "config file")
|
||||
RootCmd.PersistentFlags().StringToStringVarP(&config.Overrides, "config-value", "v", config.Overrides, "Override config values. Use dot syntax to specify key. E.g. -v output.stdout.config.format=json")
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue