Add pkg ctrl

This commit is contained in:
Neeraj Gupta 2023-09-13 14:21:05 +05:30
parent 181056d8c5
commit 57ad458662
6 changed files with 85 additions and 7 deletions

4
.gitignore vendored
View file

@ -6,4 +6,6 @@ logs/**
.vscode/**
tmp/**
scratch/**
main
main
config.yaml
ente-cli.db

View file

@ -1,6 +1,7 @@
package cmd
import (
"cli-go/pkg"
"fmt"
"github.com/spf13/viper"
"os"
@ -10,6 +11,8 @@ import (
const AppVersion = "0.0.1"
var ctrl *pkg.ClICtrl
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ente-cli",
@ -24,7 +27,8 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func Execute(controller *pkg.ClICtrl) {
ctrl = controller
err := rootCmd.Execute()
if err != nil {
os.Exit(1)

View file

@ -2,7 +2,6 @@ package api
import (
"context"
"fmt"
"github.com/google/uuid"
)
@ -54,7 +53,7 @@ func (c *Client) VerifySRPSession(
"sessionID": sessionID.String(),
"srpM1": clientM1,
}
r, err := c.restClient.R().
_, err := c.restClient.R().
SetContext(ctx).
SetResult(&res).
SetBody(payload).
@ -62,6 +61,5 @@ func (c *Client) VerifySRPSession(
if err != nil {
return nil, err
}
fmt.Sprintf("%+v", r.RawResponse)
return &res, nil
}

38
main.go
View file

@ -3,8 +3,42 @@ Copyright © 2023 NAME HERE <EMAIL ADDRESS>
*/
package main
import "cli-go/cmd"
import (
"cli-go/cmd"
"cli-go/internal/api"
"cli-go/pkg"
"fmt"
bolt "go.etcd.io/bbolt"
"log"
)
var db *bolt.DB
var client = api.NewClient(api.Params{
Debug: true,
})
func main() {
cmd.Execute()
db, err := pkg.GetDB("ente-cli.db")
if err != nil {
panic(err)
}
db.Update(func(tx *bolt.Tx) error {
log.Println("creating #AccBucket")
_, err := tx.CreateBucketIfNotExists([]byte(pkg.AccBucket))
if err != nil {
return fmt.Errorf("create bucket: %s", err)
}
return nil
})
ctrl := pkg.ClICtrl{
Client: client,
DB: db,
}
defer func() {
log.Println("closing db")
if err := db.Close(); err != nil {
panic(err)
}
}()
cmd.Execute(&ctrl)
}

View file

@ -1 +1,30 @@
package pkg
import (
"context"
"encoding/json"
bolt "go.etcd.io/bbolt"
)
const AccBucket = "accounts"
type AccountInfo struct {
Email string `json:"email" binding:"required"`
UserID int64 `json:"userID" binding:"required"`
}
func (c *ClICtrl) AddAccount(cxt context.Context, email string, userID int64) error {
return c.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(AccBucket))
info := AccountInfo{
Email: email,
UserID: userID,
}
value, err := json.Marshal(info)
if err != nil {
return err
}
err = b.Put([]byte(email), value)
return err
})
}

11
pkg/controller.go Normal file
View file

@ -0,0 +1,11 @@
package pkg
import (
"cli-go/internal/api"
bolt "go.etcd.io/bbolt"
)
type ClICtrl struct {
Client *api.Client
DB *bolt.DB
}