diff --git a/internal/command/silo.go b/internal/command/silo.go index 6083df5f..79e0f4e6 100644 --- a/internal/command/silo.go +++ b/internal/command/silo.go @@ -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", diff --git a/internal/core/silo_manager.go b/internal/core/silo_manager.go index c5f2033b..bab77bbe 100644 --- a/internal/core/silo_manager.go +++ b/internal/core/silo_manager.go @@ -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 { diff --git a/internal/sql/users.sql.go b/internal/sql/users.sql.go index 097f00d1..ea6fd2a1 100644 --- a/internal/sql/users.sql.go +++ b/internal/sql/users.sql.go @@ -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 ` diff --git a/internal/user/user_manager.go b/internal/user/user_manager.go index 404e5f70..0efe6084 100644 --- a/internal/user/user_manager.go +++ b/internal/user/user_manager.go @@ -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) +} diff --git a/sql/queries/users.sql b/sql/queries/users.sql index f0768717..44f4ee3c 100644 --- a/sql/queries/users.sql +++ b/sql/queries/users.sql @@ -8,4 +8,7 @@ RETURNING *; -- name: UserByUsername :one -SELECT * from users WHERE username = $1; \ No newline at end of file +SELECT * from users WHERE username = $1; + +-- name: UserById :one +SELECT * from users WHERE id = $1; \ No newline at end of file