tangled
alpha
login
or
join now
quietengineer.fyi
/
pdsadmin
0
fork
atom
Go implementation of pdsadmin cli
0
fork
atom
overview
issues
1
pulls
pipelines
feat: add createInviteCode command
quietengineer.fyi
10 months ago
2aa10ca7
d67eea4b
+101
2 changed files
expand all
collapse all
unified
split
pdsadmin
cmd
createInviteCode.go
sample.config.yaml
+99
pdsadmin/cmd/createInviteCode.go
···
1
1
+
/*
2
2
+
Copyright © 2025 QuietEngineer <qtengineer@proton.me>
3
3
+
4
4
+
Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
+
of this software and associated documentation files (the "Software"), to deal
6
6
+
in the Software without restriction, including without limitation the rights
7
7
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
8
+
copies of the Software, and to permit persons to whom the Software is
9
9
+
furnished to do so, subject to the following conditions:
10
10
+
11
11
+
The above copyright notice and this permission notice shall be included in
12
12
+
all copies or substantial portions of the Software.
13
13
+
14
14
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
15
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
16
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
17
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
18
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
19
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
+
THE SOFTWARE.
21
21
+
*/
22
22
+
package cmd
23
23
+
24
24
+
import (
25
25
+
"bytes"
26
26
+
"encoding/json"
27
27
+
"fmt"
28
28
+
"io"
29
29
+
"net/http"
30
30
+
"os"
31
31
+
32
32
+
"github.com/spf13/cobra"
33
33
+
"github.com/spf13/viper"
34
34
+
)
35
35
+
36
36
+
type ServerCreateInviteCode_Input struct {
37
37
+
UseCount int `json:"useCount"`
38
38
+
}
39
39
+
40
40
+
type ServerCreateInviteCode_Output struct {
41
41
+
Code string `json:"code"`
42
42
+
}
43
43
+
44
44
+
var number *int
45
45
+
46
46
+
// createInviteCodeCmd represents the createInviteCode command
47
47
+
var createInviteCodeCmd = &cobra.Command{
48
48
+
Use: "createInviteCode",
49
49
+
Short: "Create a new invite code",
50
50
+
Example: "pdsadmin createInviteCode",
51
51
+
Args: cobra.NoArgs,
52
52
+
Run: func(cmd *cobra.Command, args []string) {
53
53
+
if *number < 1 {
54
54
+
fmt.Println("number must be >=1")
55
55
+
os.Exit(1)
56
56
+
}
57
57
+
body := ServerCreateInviteCode_Input{
58
58
+
UseCount: *number,
59
59
+
}
60
60
+
jsonBody, err := json.Marshal(body)
61
61
+
if err != nil {
62
62
+
fmt.Printf("could not create json body: %s\n", err)
63
63
+
os.Exit(1)
64
64
+
}
65
65
+
bodyReader := bytes.NewReader(jsonBody)
66
66
+
67
67
+
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("https://%s/xrpc/com.atproto.server.createInviteCode", viper.GetString("hostname")), bodyReader)
68
68
+
if err != nil {
69
69
+
fmt.Printf("could not create request: %s\n", err)
70
70
+
os.Exit(1)
71
71
+
}
72
72
+
req.Header.Add("Content-Type", "application/json")
73
73
+
req.SetBasicAuth("admin", viper.GetString("admin_password"))
74
74
+
75
75
+
client := &http.Client{}
76
76
+
res, err := client.Do(req)
77
77
+
if err != nil {
78
78
+
fmt.Printf("error making http request: %s\n", err)
79
79
+
os.Exit(1)
80
80
+
}
81
81
+
resBody, err := io.ReadAll(res.Body)
82
82
+
if err != nil {
83
83
+
fmt.Printf("could not read response body: %s\n", err)
84
84
+
os.Exit(1)
85
85
+
}
86
86
+
87
87
+
var inviteCode ServerCreateInviteCode_Output
88
88
+
if err := json.Unmarshal(resBody, &inviteCode); err != nil {
89
89
+
fmt.Printf("could not get invite code: %s\n", err)
90
90
+
os.Exit(1)
91
91
+
}
92
92
+
fmt.Println(inviteCode.Code)
93
93
+
},
94
94
+
}
95
95
+
96
96
+
func init() {
97
97
+
rootCmd.AddCommand(createInviteCodeCmd)
98
98
+
number = createInviteCodeCmd.Flags().IntP("number", "n", 1, "number of times the code can be used")
99
99
+
}
+2
pdsadmin/sample.config.yaml
···
1
1
+
admin_password: a_secure_password
2
2
+
hostname: pds.example.com # without protocol (https) or path