-
Notifications
You must be signed in to change notification settings - Fork 4
/
create.go
132 lines (111 loc) · 3.45 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright 2023 The Bestchains Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package repository
import (
"fmt"
"log"
"net/http"
"net/url"
"github.com/bestchains/bc-cli/pkg/account"
"github.com/bestchains/bc-cli/pkg/common"
"github.com/bestchains/bc-cli/pkg/nonce"
uhttp "github.com/bestchains/bc-cli/pkg/utils/http"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func NewCreateMarketRepoCmd() *cobra.Command {
var err error
cmd := &cobra.Command{
Use: "market repo [args]",
Short: "Create market",
RunE: func(cmd *cobra.Command, args []string) error {
// WalletDir&Account will be used to init the wallet
walletDir, err := cmd.Flags().GetString("wallet")
if err != nil {
return err
}
accountAddress, err := cmd.Flags().GetString("account")
if err != nil {
return err
}
// read repository url
repoURL, err := cmd.Flags().GetString("url")
if err != nil {
return err
}
// bind depository server to flag
_ = viper.BindPFlag("saas.market.server", cmd.Flags().Lookup("host"))
host := viper.GetString("saas.market.server")
if host == "" {
return fmt.Errorf("no host provided")
}
fmt.Printf("creating repository with account %s endorsement \n", accountAddress)
resp, err := CreateRepo(host, walletDir, accountAddress, repoURL)
if err != nil {
return err
}
fmt.Print(string(resp))
return nil
},
}
// define flags
cmd.Flags().StringP("host", "", "http://localhost:9998", "host URL of market server")
cmd.Flags().StringP("wallet", "w", common.DefaultWalletConfigDir, "wallet path")
cmd.Flags().StringP("account", "a", "", "account to be used")
cmd.Flags().String("repo-url", "", "repository url")
// define required flags
err = cmd.MarkFlagRequired("account")
if err != nil {
log.Fatal(err)
}
err = cmd.MarkFlagRequired("repo-url")
if err != nil {
log.Fatal(err)
}
return cmd
}
// CreateRepo creates a new repository on the specified host using the provided account and repo URL.
// It returns the response body as a byte slice and any error encountered.
func CreateRepo(host string, walletDir string, accountAddress string, repoURL string) ([]byte, error) {
// Read account info.
wallet, err := account.NewLocalWallet(walletDir)
if err != nil {
return nil, err
}
acc, err := wallet.GetAccount(accountAddress)
if err != nil {
return nil, err
}
// Get nonce.
currNonce, err := nonce.Get(host, common.MarketCurrentNonce, acc.Address)
if err != nil {
return nil, err
}
// Generate message.
msgBase64, err := acc.GenerateAndSignMessage(currNonce, repoURL)
if err != nil {
return nil, err
}
// POST PutValue request.
postValue := url.Values{}
postValue.Add("message", msgBase64)
postValue.Add("url", repoURL)
host = fmt.Sprintf("%s%s", host, common.CreateRepository)
resp, err := uhttp.Do(host, http.MethodPost, map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}, []byte(postValue.Encode()))
if err != nil {
return nil, err
}
return resp, nil
}