Skip to content

Commit

Permalink
add firestore sample insert logic
Browse files Browse the repository at this point in the history
  • Loading branch information
AshutoshPatole committed Aug 10, 2024
1 parent 0194b27 commit b85ddb0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
18 changes: 0 additions & 18 deletions cmd/pull.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package cmd

import (
"cloud.google.com/go/firestore"
"context"
firebase "firebase.google.com/go"
"fmt"
"log"
"ssm-v2/internal/store"

"github.com/spf13/cobra"
Expand All @@ -29,17 +25,3 @@ var pullCmd = &cobra.Command{
func init() {
syncCmd.AddCommand(pullCmd)
}

func test(app *firebase.App) {
client, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("error getting Firestore client: %v", err)
}
defer func(client *firestore.Client) {
err := client.Close()
if err != nil {
return
}
}(client)

}
45 changes: 35 additions & 10 deletions cmd/push.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package cmd

import (
"cloud.google.com/go/firestore"
"context"
"fmt"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"log"
"ssm-v2/internal/store"
)

// pushCmd represents the push command
Expand All @@ -12,20 +16,41 @@ var pushCmd = &cobra.Command{
Short: "A brief description of your command",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("push called")
user, err := store.LoginUser(userEmail, userPassword)
if err != nil {
fmt.Println(err)
return
}

userId := user["user_id"].(string)

upload(userId)
},
}

func init() {
syncCmd.AddCommand(pushCmd)
}

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// pushCmd.PersistentFlags().String("foo", "", "A help for foo")
func upload(documentID string) {
client, err := store.App.Firestore(context.Background())
if err != nil {
log.Fatalf("error getting Firestore client: %v", err)
}
defer func(client *firestore.Client) {
err := client.Close()
if err != nil {
return
}
}(client)

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// pushCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
configurations := client.Collection("configurations")
//configurations.
_, err = configurations.Doc(documentID).Create(context.Background(), map[string]interface{}{
"something": "with nothing",
})
if err != nil {
logrus.Fatalf("error adding configuration: %v", err)
}
logrus.Info("Configuration uploaded with reference %s", documentID)
}
10 changes: 10 additions & 0 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"github.com/spf13/cobra"
)

var (
userEmail string
userPassword string
)

// syncCmd represents the sync command
var syncCmd = &cobra.Command{
Use: "sync",
Expand All @@ -15,4 +20,9 @@ var syncCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(syncCmd)
syncCmd.PersistentFlags().StringVarP(&userEmail, "email", "e", "", "email address")
syncCmd.PersistentFlags().StringVarP(&userPassword, "password", "p", "", "password")
_ = syncCmd.MarkPersistentFlagRequired("email")
_ = syncCmd.MarkPersistentFlagRequired("password")

}
1 change: 0 additions & 1 deletion internal/store/firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func parseToken(tokenString string) map[string]interface{} {
if err != nil {
log.Fatalf("Error parsing token: %v", err)
}
fmt.Println(token)
if claims, ok := token.Claims.(jwt.MapClaims); ok {
result := make(map[string]interface{})
if email, ok := claims["email"]; ok {
Expand Down

0 comments on commit b85ddb0

Please sign in to comment.