diff --git a/cmd/cromp/client.go b/cmd/cromp/client.go new file mode 100644 index 0000000..f9b48ec --- /dev/null +++ b/cmd/cromp/client.go @@ -0,0 +1,111 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" +) + +// Get is a generic HTTP GET handler +func Get(url string, resBody interface{}) error { + var req *http.Request + client := http.DefaultClient + buf := new(bytes.Buffer) + + cfg := &Config{} + err := cfg.ReadConfig() + if err != nil { + return err + } + + req, err = http.NewRequest("GET", fmt.Sprintf("%s%s", cfg.URL, url), buf) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Access-Token", cfg.Token) + + resp, err := client.Do(req) + if err != nil { + return err + } + + defer resp.Body.Close() + + if resp.StatusCode > 199 && resp.StatusCode < 300 { + if err = json.NewDecoder(resp.Body).Decode(&resBody); err != nil { + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + fmt.Println(string(data)) + + return err + } + } else { + data, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + + fmt.Println(string(data)) + } + + return nil +} + +// Post is a generic HTTP POST handler +func Post(url string, reqBody, resBody interface{}) (err error) { + var req *http.Request + client := http.DefaultClient + buf := new(bytes.Buffer) + + cfg := &Config{} + err = cfg.ReadConfig() + if err != nil { + return err + } + + if err := json.NewEncoder(buf).Encode(reqBody); err != nil { + return err + } + + req, err = http.NewRequest("POST", fmt.Sprintf("%s%s", cfg.URL, url), buf) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Access-Token", cfg.Token) + + res, err := client.Do(req) + if err != nil { + return err + } + + defer res.Body.Close() + + if res.StatusCode > 199 && res.StatusCode < 300 { + if err = json.NewDecoder(res.Body).Decode(&resBody); err != nil { + data, err := ioutil.ReadAll(res.Body) + if err != nil { + return err + } + + fmt.Println(string(data)) + return err + } + } else { + data, err := ioutil.ReadAll(res.Body) + if err != nil { + return err + } + + fmt.Println(string(data)) + } + + return nil +} diff --git a/cmd/cromp/config.go b/cmd/cromp/config.go new file mode 100644 index 0000000..a6f5f00 --- /dev/null +++ b/cmd/cromp/config.go @@ -0,0 +1,55 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "os/user" + "path" +) + +func getPath() (*string, error) { + usr, err := user.Current() + if err != nil { + return nil, err + } + p := path.Join(usr.HomeDir, ".cromprc") + + return &p, nil +} + +// Config represents the client configuration for cromp +type Config struct { + Token string `json:"token"` + URL string `json:"url"` +} + +// WriteConfig dumbs the configuration to disk +func (c *Config) WriteConfig() error { + + b, err := json.Marshal(c) + if err != nil { + return err + } + + p, err := getPath() + if err != nil { + return err + } + + return ioutil.WriteFile(*p, b, 0600) +} + +// ReadConfig reads a configuration from disk +func (c *Config) ReadConfig() error { + p, err := getPath() + if err != nil { + return err + } + + data, err := ioutil.ReadFile(*p) + if err != nil { + return err + } + + return json.Unmarshal(data, c) +} diff --git a/cmd/cromp/main.go b/cmd/cromp/main.go index 8891354..19058d6 100644 --- a/cmd/cromp/main.go +++ b/cmd/cromp/main.go @@ -5,9 +5,11 @@ import ( "flag" "fmt" "os" - "text/tabwriter" + "strings" + "github.com/google/uuid" "github.com/peterbourgon/ff/v2/ffcli" + "suah.dev/cromp/db" cromp "suah.dev/cromp/internal" ) @@ -18,6 +20,8 @@ func main() { loadFlagSet = flag.NewFlagSet("cromp load", flag.ExitOnError) configFlagSet = flag.NewFlagSet("cromp config", flag.ExitOnError) + urlConfFS = configFlagSet.String("url", "", "URL of cromp server") + tokenConfFS = configFlagSet.String("token", "", "Access token for cromp server") //createFlagSet = flag.NewFlagSet("cromp create", flag.ExitOnError) ) @@ -28,12 +32,64 @@ func main() { Subcommands: []*ffcli.Command{ &ffcli.Command{ Name: "config", - ShortUsage: "cromp config key=value", + ShortUsage: "cromp config -token [token] -url [url]", FlagSet: configFlagSet, Exec: func(ctx context.Context, args []string) error { - if len(args) < 1 { - return fmt.Errorf("") + cfg := &Config{ + Token: *tokenConfFS, + URL: *urlConfFS, } + + err := cfg.WriteConfig() + if err != nil { + return err + } + + return nil + }, + }, + &ffcli.Command{ + Name: "similar", + ShortUsage: "cromp similar [text]", + Exec: func(ctx context.Context, args []string) error { + var params db.SimilarEntriesParams + resp := &[]db.SimilarEntriesRow{} + + params.Similarity = strings.Join(args, " ") + + err := Post("/entries/similar", params, resp) + if err != nil { + return err + } + + for _, e := range *resp { + fmt.Printf("%s\t%s\t%f\n", + e.EntryID, + e.Title, + e.Similarity) + } + + return nil + }, + }, + &ffcli.Command{ + Name: "list", + ShortUsage: "cromp list", + FlagSet: configFlagSet, + Exec: func(ctx context.Context, args []string) error { + resp := &[]db.Entry{} + + err := Get("/entries/list", resp) + if err != nil { + return err + } + + for _, e := range *resp { + fmt.Printf("%s\t%s\t%s\n", e.EntryID.String(), + e.CreatedAt, + e.Title) + } + return nil }, }, @@ -42,6 +98,9 @@ func main() { ShortUsage: "cromp load ", FlagSet: loadFlagSet, Exec: func(ctx context.Context, args []string) error { + var entry db.CreateEntryParams + resp := &db.CreateEntryRow{} + if len(args) != 1 { return fmt.Errorf("missing file name") } @@ -50,12 +109,30 @@ func main() { return err } - w := tabwriter.NewWriter(os.Stdout, 0, 0, 4, ' ', 0) - fmt.Fprintf(w, "Title:\t%s\n", header.Title) - fmt.Fprintf(w, "Author:\t%s\n", header.Author) - fmt.Fprintf(w, "Date:\t%s\n", header.Date) - fmt.Fprintf(w, "UUID:\t%s\n", header.UUID) - w.Flush() + s := header.UUID.String() + if s == "" || s == "00000000-0000-0000-0000-000000000000" { + + header.UUID = uuid.New() + } + + entry.Title = header.Title + entry.EntryID = header.UUID + + fmt.Printf("%#v\n", entry) + + data, err := cromp.ReadFileBody(args[0]) + if err != nil { + return err + } + + entry.Body = string(data) + + err = Post("/entries/add", entry, resp) + if err != nil { + return err + } + + fmt.Printf("%#v\n", resp) return nil },