Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
feat: read project id from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Dec 3, 2019
1 parent 622f2f4 commit 705aa23
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions firebase/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package firebase

import (
"context"
"encoding/json"
"fmt"
"log"

firebase "firebase.google.com/go"
"firebase.google.com/go/auth"
s "github.com/bitfield/script"
)

const firebaseProjectConfig string = "./.firebaserc"
const defaultProject = "default"

type Firebase struct {
app *firebase.App
projectId string
}

func (f *Firebase) setProjectID(projectId string) error {

configFileContent, err := s.File(firebaseProjectConfig).Bytes()

if err != nil {
return fmt.Errorf("An error occurred while reading config file: %w", err)
}

var decodedConfigs *FirebaseProjectConfigs

err = json.Unmarshal(configFileContent, &decodedConfigs)

if err != nil {
return fmt.Errorf("An error occurred while reading config file: %w", err)
}

f.projectId = decodedConfigs.Projects[projectId]

return nil
}

func (f *Firebase) InitializeFirbeaseApp(ctx context.Context, projectId string) error {

if projectId == "" {
projectId = defaultProject
}

err := f.setProjectID(projectId)

if err != nil {
return fmt.Errorf("An error occurred while reading config file: %w", err)
}

configs := &firebase.Config{
ProjectID: f.projectId,
}

app, err := firebase.NewApp(ctx, configs)

if err != nil {
log.Fatalf("error initializing app: %v\n", err)
}

f.app = app

return nil
}

func (f *Firebase) auth(ctx context.Context) (*auth.Client, error) {
return f.app.Auth(ctx)
}

0 comments on commit 705aa23

Please sign in to comment.