nastra commented on code in PR #58:
URL: https://github.com/apache/iceberg-go/pull/58#discussion_r1487436435


##########
cmd/iceberg/main.go:
##########
@@ -0,0 +1,337 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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 main
+
+import (
+       "context"
+       "errors"
+       "fmt"
+       "log"
+       "os"
+       "strings"
+
+       "github.com/apache/iceberg-go"
+       "github.com/apache/iceberg-go/catalog"
+       "github.com/apache/iceberg-go/table"
+       "github.com/docopt/docopt-go"
+)
+
+const usage = `iceberg.
+
+Usage:
+  iceberg list [options] [PARENT]
+  iceberg describe [options] [namespace | table] IDENTIFIER
+  iceberg (schema | spec | uuid | location) [options] TABLE_ID
+  iceberg drop [options] (namespace | table) IDENTIFIER
+  iceberg files [options] TABLE_ID [--history]
+  iceberg rename [options] <from> <to>
+  iceberg properties [options] get (namespace | table) IDENTIFIER [PROPNAME]
+  iceberg properties [options] set (namespace | table) IDENTIFIER PROPNAME 
VALUE
+  iceberg properties [options] remove (namespace | table) IDENTIFIER PROPNAME
+  iceberg -h | --help | --version
+
+Arguments:
+  PARENT         Catalog parent namespace
+  IDENTIFIER     fully qualified namespace or table
+  TABLE_ID       full path to a table
+  PROPNAME       name of a property
+  VALUE          value to set
+
+Options:
+  -h --help          show this helpe messages and exit
+  --catalog TEXT     specify the catalog type [default: rest]
+  --uri TEXT         specify the catalog URI
+  --output TYPE      output type (json/text) [default: text]
+  --credential TEXT  specify credentials for the catalog`
+
+func main() {
+       args, err := docopt.ParseArgs(usage, os.Args[1:], iceberg.Version())
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       cfg := struct {
+               List     bool `docopt:"list"`
+               Describe bool `docopt:"describe"`
+               Schema   bool `docopt:"schema"`
+               Spec     bool `docopt:"spec"`
+               Uuid     bool `docopt:"uuid"`
+               Location bool `docopt:"location"`
+               Props    bool `docopt:"properties"`
+               Drop     bool `docopt:"drop"`
+               Files    bool `docopt:"files"`
+               Rename   bool `docopt:"rename"`
+
+               Get    bool `docopt:"get"`
+               Set    bool `docopt:"set"`
+               Remove bool `docopt:"remove"`
+
+               Namespace bool `docopt:"namespace"`
+               Table     bool `docopt:"table"`
+
+               RenameFrom string `docopt:"<from>"`
+               RenameTo   string `docopt:"<to>"`
+
+               Parent   string `docopt:"PARENT"`
+               Ident    string `docopt:"IDENTIFIER"`
+               TableID  string `docopt:"TABLE_ID"`
+               PropName string `docopt:"PROPNAME"`
+               Value    string `docopt:"VALUE"`
+
+               Catalog string `docopt:"--catalog"`
+               URI     string `docopt:"--uri"`
+               Output  string `docopt:"--output"`
+               History bool   `docopt:"--history"`
+               Cred    string `docopt:"--credential"`
+       }{}
+
+       if err := args.Bind(&cfg); err != nil {
+               log.Fatal(err)
+       }
+
+       var output Output
+       switch strings.ToLower(cfg.Output) {
+       case "text":
+               output = text{}
+       case "json":
+               fallthrough
+       default:
+               log.Fatal("unimplemented output type")
+       }
+
+       opts := []catalog.Option{}
+       if len(cfg.Cred) > 0 {
+               opts = append(opts, catalog.WithCredential(cfg.Cred))
+       }
+
+       var cat catalog.Catalog
+       switch catalog.CatalogType(cfg.Catalog) {
+       case catalog.REST:
+               if cat, err = catalog.NewRestCatalog("rest", cfg.URI, opts...); 
err != nil {
+                       log.Fatal(err)
+               }
+       default:
+               log.Fatal("unrecognized catalog type")
+       }
+
+       switch {
+       case cfg.List:
+               list(output, cat, cfg.Parent)
+       case cfg.Describe:
+               entityType := "any"
+               if cfg.Namespace {
+                       entityType = "ns"
+               } else if cfg.Table {
+                       entityType = "tbl"
+               }
+
+               describe(output, cat, cfg.Ident, entityType)
+       case cfg.Schema:
+               tbl := loadTable(output, cat, cfg.TableID)
+               output.Schema(tbl.Schema())
+       case cfg.Spec:
+               tbl := loadTable(output, cat, cfg.TableID)
+               output.Spec(tbl.Spec())
+       case cfg.Location:
+               tbl := loadTable(output, cat, cfg.TableID)
+               output.Text(tbl.Location())
+       case cfg.Uuid:
+               tbl := loadTable(output, cat, cfg.TableID)
+               output.Uuid(tbl.Metadata().TableUUID())
+       case cfg.Props:
+               properties(output, cat, propCmd{
+                       get: cfg.Get, set: cfg.Set, remove: cfg.Remove,
+                       namespace: cfg.Namespace, table: cfg.Table,
+                       identifier: cfg.Ident,
+                       propname:   cfg.PropName,
+                       value:      cfg.Value,
+               })
+       case cfg.Rename:
+               _, err := cat.RenameTable(context.Background(),
+                       catalog.ToRestIdentifier(cfg.RenameFrom), 
catalog.ToRestIdentifier(cfg.RenameTo))
+               if err != nil {
+                       output.Error(err)
+                       os.Exit(1)
+               }
+
+               output.Text("Renamed table from " + cfg.RenameFrom + " to " + 
cfg.RenameTo)
+       case cfg.Drop:
+               switch {
+               case cfg.Namespace:

Review Comment:
   given that we can drop a namespace, should there be an option to create a 
namespace? This can be done in a follow-up



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to