-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acf5f3e
commit 835a527
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/streamingfast/cli/sflags" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
. "github.com/streamingfast/cli" | ||
"github.com/streamingfast/substreams-sink-sql/db" | ||
) | ||
|
||
var createUserCmd = Command(createUserE, | ||
"create-user <dsn> <username>", | ||
"Create a user in the database", | ||
ExactArgs(2), | ||
Flags(func(flags *pflag.FlagSet) { | ||
flags.Bool("read-only", false, "Create a read-only user") | ||
flags.String("password-env", "", "Name of the environment variable containing the password") | ||
}), | ||
) | ||
|
||
func createUserE(cmd *cobra.Command, args []string) error { | ||
ctx := cmd.Context() | ||
|
||
dsn := args[0] | ||
username := args[1] | ||
|
||
readOnly := sflags.MustGetBool(cmd, "read-only") | ||
passwordEnv := sflags.MustGetString(cmd, "password-env") | ||
|
||
if passwordEnv == "" { | ||
return fmt.Errorf("password-env is required") | ||
} | ||
|
||
password := os.Getenv(passwordEnv) | ||
if password == "" { | ||
return fmt.Errorf("non-empty password is required") | ||
} | ||
|
||
dbLoader, err := db.NewLoader(dsn, 0, db.OnModuleHashMismatchError, nil, zlog, tracer) | ||
if err != nil { | ||
return fmt.Errorf("new psql loader: %w", err) | ||
} | ||
|
||
err = dbLoader.CreateUser(ctx, username, password, "substreams", readOnly) | ||
if err != nil { | ||
return fmt.Errorf("create user: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters