Skip to content

Commit

Permalink
Merge pull request #4 from hypha-dao/migration
Browse files Browse the repository at this point in the history
migration
  • Loading branch information
mgravitt authored Mar 4, 2021
2 parents 7eca498 + 3e5c2dd commit f98b014
Show file tree
Hide file tree
Showing 24 changed files with 505 additions and 586 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ test-payloads/
dist/
daoctl-*.yaml
daoctl-local.yaml
last-doc.tmp
last-doc.tmp
.graph.cache
76 changes: 30 additions & 46 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (
"time"

"github.com/eoscanada/eos-go"
"github.com/hypha-dao/daoctl/models"
"github.com/hypha-dao/document-graph/docgraph"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var backupCmd = &cobra.Command{
Use: "backup",
Short: "creates a local backup of current DAO data",
Long: "creates a new directory containing JSON files for all DAO object types",
Short: "creates a local backup of the environment, including all documents and edges",
Long: "creates a local backup of the environment, including all documents and edges",
// Args: cobra.RangeArgs(1, 1),
Run: func(cmd *cobra.Command, args []string) {
RunE: func(cmd *cobra.Command, args []string) error {
api := getAPI()
ctx := context.Background()
contract := eos.AN(viper.GetString("DAOContract"))

folderName := viper.GetString("backup-cmd-output-dir") + "/dao-backup-" + time.Now().Format("2006Jan02-150405")

Expand All @@ -32,54 +33,37 @@ var backupCmd = &cobra.Command{
}
fmt.Println("\nBacking up to folder: ", folderName)

var request eos.GetTableByScopeRequest
request.Code = viper.GetString("DAOContract")
request.Table = "objects"
request.Limit = 500 // maximum number of scopes
response, err := api.GetTableByScope(context.Background(), request)
errorCheck("get table by scope", err)

var scopes []models.Scope
json.Unmarshal(response.Rows, &scopes)

for _, scope := range scopes {
saveObjects(ctx, api, folderName, string(scope.Scope), "objects")
documents, err := docgraph.GetAllDocuments(ctx, api, contract)
if err != nil {
return fmt.Errorf("cannot get all documents: %v", err)
}

saveObjects(ctx, api, folderName, viper.GetString("DAOContract"), "periods")
saveObjects(ctx, api, folderName, viper.GetString("DAOContract"), "config")
saveObjects(ctx, api, folderName, viper.GetString("DAOContract"), "applicants")
saveObjects(ctx, api, folderName, viper.GetString("DAOContract"), "members")
saveObjects(ctx, api, folderName, viper.GetString("DAOContract"), "payments")
},
}
documentsJson, err := json.MarshalIndent(documents, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal documents to json: %v", err)
}

func saveObjects(ctx context.Context, api *eos.API, folderName, scope, table string) {
err = ioutil.WriteFile("documents.json", documentsJson, 0644)
if err != nil {
return fmt.Errorf("cannot documents to documents.json file: %v", err)
}

filename := folderName + "/" + scope + "_" + table + ".json"
edges, err := docgraph.GetAllEdges(ctx, api, contract)
if err != nil {
return fmt.Errorf("cannot get all edges: %v", err)
}

var request eos.GetTableRowsRequest
request.Code = viper.GetString("DAOContract")
request.Scope = scope
request.Table = table
request.Limit = 1000
request.JSON = true
response, err := api.GetTableRows(ctx, request)
if err != nil {
fmt.Println("Unable to retrieve rows: ", scope)
panic(err)
}
edgesJson, err := json.MarshalIndent(edges, "", " ")
if err != nil {
return fmt.Errorf("cannot marshal edges to json: %v", err)
}

data, err := response.Rows.MarshalJSON()
if err != nil {
fmt.Println("Unable to backup scope: ", scope, ", table: ", table)
panic(err)
}
err = ioutil.WriteFile(filename, data, 0644)
if err != nil {
fmt.Println("Unable to write file: ", filename, " for scope: ", scope, ", table: ", table)
panic(err)
}
err = ioutil.WriteFile("edges.json", edgesJson, 0644)
if err != nil {
return fmt.Errorf("cannot edges to edges.json file: %v", err)
}
return nil
},
}

func init() {
Expand Down
42 changes: 42 additions & 0 deletions cmd/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

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

eos "github.com/eoscanada/eos-go"
"github.com/hypha-dao/daoctl/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/pretty"
)

var dumpCmd = &cobra.Command{
Use: "dump [hash]",
Short: "raw dump of the documents json",
Long: "raw dump of the documents json",
Args: cobra.RangeArgs(1, 1),
RunE: func(cmd *cobra.Command, args []string) error {
api := eos.New(viper.GetString("EosioEndpoint"))
ctx := context.Background()
contract := eos.AN(viper.GetString("DAOContract"))

document, err := util.Get(ctx, api, contract, args[0])
if err != nil {
return fmt.Errorf("cannot find document with hash: %v %v", args[0], err)
}

docJson, err := json.Marshal(document)
if err != nil {
return fmt.Errorf("cannot marshall document to JSON: %v %v", args[0], err)
}

fmt.Println(string(pretty.Color(pretty.Pretty(docJson), nil)))
return nil
},
}

func init() {
RootCmd.AddCommand(dumpCmd)
}
51 changes: 51 additions & 0 deletions cmd/fix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cmd

import (
"context"
"fmt"
"strconv"

eos "github.com/eoscanada/eos-go"
"github.com/hypha-dao/daoctl/util"
"github.com/hypha-dao/document-graph/docgraph"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type period struct {
Proposer eos.AccountName `json:"proposer"`
ProposalType eos.Name `json:"proposal_type"`
ContentGroups []docgraph.ContentGroup `json:"content_groups"`
}

// Period represents a period of time aligning to a payroll period, typically a week
type Period struct {
PeriodID uint64 `json:"period_id"`
StartTime eos.BlockTimestamp `json:"start_date"`
EndTime eos.BlockTimestamp `json:"end_date"`
Phase string `json:"phase"`
}

var fixCmd = &cobra.Command{
Use: "fix",
Short: "fix a document - admin only",

RunE: func(cmd *cobra.Command, args []string) error {

ctx := context.Background()
api := getAPI()
contract := eos.AN(viper.GetString("DAOContract"))

gc, err := util.GetCache(ctx, api, contract)
if err != nil {
return fmt.Errorf("cannot get cache: %v", err)
}
fmt.Println("Number of items in the cache: " + strconv.Itoa(gc.Cache.ItemCount()))
return nil
},
}

func init() {
RootCmd.AddCommand(fixCmd)
}
67 changes: 32 additions & 35 deletions cmd/getAssignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,62 @@ package cmd

import (
"context"
"encoding/csv"
"fmt"
"log"
"os"

"github.com/alexeyco/simpletable"
"github.com/eoscanada/eos-go"
"github.com/hypha-dao/daoctl/models"
"github.com/hypha-dao/daoctl/views"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var getAssignmentsCmd = &cobra.Command{
Use: "assignments",
Short: "retrieve assignments",
Long: "retrieve and print assignment tables",
Run: func(cmd *cobra.Command, args []string) {
api := eos.New(viper.GetString("EosioEndpoint"))
ctx := context.Background()
// api := eos.New(viper.GetString("EosioEndpoint"))
// ctx := context.Background()

periods := models.LoadPeriods(api, true, true)
roles := models.Roles(ctx, api, periods, "role")
includeExpired := viper.GetBool("get-assignments-cmd-expired")
// periods := models.LoadPeriods(api, true, true)
// roles := models.Roles(ctx, api, "role")
// includeExpired := viper.GetBool("get-assignments-cmd-expired")

if viper.GetBool("global-csv") {
assignmentsTable := getAssignmentTable(ctx, api, roles, periods, "Active Assignment", "assignment", includeExpired)
csvData := models.TableToData(assignmentsTable)
// if viper.GetBool("global-csv") {
// assignmentsTable := getAssignmentTable(ctx, api, roles, periods, "Active Assignment", "assignment", includeExpired)
// csvData := models.TableToData(assignmentsTable)

file, err := os.Create(viper.GetString("global-output-file"))
if err != nil {
log.Fatalln("error writing csv:", err)
}
// file, err := os.Create(viper.GetString("global-output-file"))
// if err != nil {
// log.Fatalln("error writing csv:", err)
// }

defer file.Close()
// defer file.Close()

w := csv.NewWriter(file)
w.WriteAll(csvData) // calls Flush internally
// w := csv.NewWriter(file)
// w.WriteAll(csvData) // calls Flush internally

if err := w.Error(); err != nil {
log.Fatalln("error writing csv:", err)
}
} else {
if viper.GetBool("global-active") == true {
printAssignmentTable(ctx, api, roles, periods, "Active Assignment", "assignment", includeExpired)
}
// if err := w.Error(); err != nil {
// log.Fatalln("error writing csv:", err)
// }
// } else {
// if viper.GetBool("global-active") == true {
// printAssignmentTable(ctx, api, roles, periods, "Active Assignment", "assignment", includeExpired)
// }

if viper.GetBool("global-include-proposals") == true {
printAssignmentTable(ctx, api, roles, periods, "Current Assignment Proposals", "proposal", includeExpired)
}
// if viper.GetBool("global-include-proposals") == true {
// printAssignmentTable(ctx, api, roles, periods, "Current Assignment Proposals", "proposal", includeExpired)
// }

if viper.GetBool("global-failed-proposals") == true {
printAssignmentTable(ctx, api, roles, periods, "Failed Assignment Proposals", "failedprops", includeExpired)
}
// if viper.GetBool("global-failed-proposals") == true {
// printAssignmentTable(ctx, api, roles, periods, "Failed Assignment Proposals", "failedprops", includeExpired)
// }

if viper.GetBool("global-include-archive") == true {
printAssignmentTable(ctx, api, roles, periods, "Archive of Assignment Proposals", "proparchive", includeExpired)
}
}
// if viper.GetBool("global-include-archive") == true {
// printAssignmentTable(ctx, api, roles, periods, "Archive of Assignment Proposals", "proparchive", includeExpired)
// }
// }
},
}

Expand Down
Loading

0 comments on commit f98b014

Please sign in to comment.