Silo List command

This commit is contained in:
Abhishek Shroff
2024-04-19 12:31:38 +05:30
parent af3e70c371
commit dca85cc4d9
5 changed files with 57 additions and 4 deletions

View File

@@ -15,7 +15,8 @@ func setupSiloCommand() *cobra.Command {
}
cmd.AddCommand([]*cobra.Command{
setupSiloCreateCommand(),
setupLibraryDeleteCommand(),
setupSiloListCommand(),
setupSiloDeleteCommand(),
}...)
return cmd
}
@@ -23,7 +24,7 @@ func setupSiloCommand() *cobra.Command {
func setupSiloCreateCommand() *cobra.Command {
return &cobra.Command{
Use: "create storage-backend owner display-name",
Short: "Create Library",
Short: "Create Silo",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
id := uuid.New()
@@ -48,7 +49,31 @@ func setupSiloCreateCommand() *cobra.Command {
}
}
func setupLibraryDeleteCommand() *cobra.Command {
func setupSiloListCommand() *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List Silos",
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
silos, err := fs.ListSilos(ctx)
if err != nil {
logrus.Fatal(err)
}
for _, silo := range silos {
logrus.Infof("%-16s: %s\n", silo.Name, silo.ID.String())
logrus.Infof(" storage: %s\n", silo.Storage)
if owner, err := userManager.UserById(ctx, silo.Owner); err == nil {
logrus.Infof(" owner: %5d - %s\n", silo.Owner, owner.Username)
} else {
logrus.Infof(" owner not found: %5d\n", silo.Owner)
}
}
},
}
}
func setupSiloDeleteCommand() *cobra.Command {
return &cobra.Command{
Use: "delete id",
Short: "Delete Silo",

View File

@@ -37,6 +37,10 @@ func (fs FileSystem) CreateSilo(ctx context.Context, id uuid.UUID, owner int32,
})
}
func (fs FileSystem) ListSilos(ctx context.Context) ([]sql.Silo, error) {
return fs.db.Queries().ListSilos(ctx)
}
func (fs FileSystem) OpenSilo(ctx context.Context, id uuid.UUID) (*Silo, error) {
silo, err := fs.db.Queries().SiloById(ctx, id)
if err != nil {

View File

@@ -37,6 +37,23 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
return i, err
}
const userById = `-- name: UserById :one
SELECT id, display_name, username, password_hash, deleted from users WHERE id = $1
`
func (q *Queries) UserById(ctx context.Context, id int32) (User, error) {
row := q.db.QueryRow(ctx, userById, id)
var i User
err := row.Scan(
&i.ID,
&i.DisplayName,
&i.Username,
&i.PasswordHash,
&i.Deleted,
)
return i, err
}
const userByUsername = `-- name: UserByUsername :one
SELECT id, display_name, username, password_hash, deleted from users WHERE username = $1
`

View File

@@ -25,3 +25,7 @@ func (m Manager) CreateUser(ctx context.Context, username, displayName, password
func (m Manager) FindUser(ctx context.Context, username string) (sql.User, error) {
return m.db.Queries().UserByUsername(ctx, username)
}
func (m Manager) UserById(ctx context.Context, id int32) (sql.User, error) {
return m.db.Queries().UserById(ctx, id)
}

View File

@@ -8,4 +8,7 @@ RETURNING *;
-- name: UserByUsername :one
SELECT * from users WHERE username = $1;
SELECT * from users WHERE username = $1;
-- name: UserById :one
SELECT * from users WHERE id = $1;