Skip to content

Commit

Permalink
Merge d5a5728 into eee6fd8
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW authored Apr 8, 2023
2 parents eee6fd8 + d5a5728 commit ecc24d0
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
151 changes: 151 additions & 0 deletions cmd/ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
Copyright © 2023 Ken'ichiro Oyama <k1lowxb@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd

import (
"fmt"
"os"
"strings"

"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/datasource"
"github.com/k1LoW/tbls/schema"
"github.com/minio/pkg/wildcard"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

var long bool

// lsCmd represents the ls command
var lsCmd = &cobra.Command{
Use: "ls",
Short: "list schema resources",
Long: `list schema resources.`,
RunE: func(cmd *cobra.Command, args []string) error {
c, err := config.New()
if err != nil {
return err
}

options, patterns, err := loadLsArgs(args)
if err != nil {
return err
}

if err := c.Load(configPath, options...); err != nil {
return err
}

var s *schema.Schema
if _, err := os.Stat(c.SchemaFilePath()); err == nil {
s, err = datasource.AnalyzeJSONStringOrFile(c.SchemaFilePath())
if err != nil {
return err
}
} else {
s, err = datasource.Analyze(c.DSN)
if err != nil {
return err
}
if err := c.ModifySchema(s); err != nil {
return err
}
}

if err := c.FilterTables(s); err != nil {
return err
}

table := tablewriter.NewWriter(os.Stdout)
table.SetAutoWrapText(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
if long {
table.SetHeader([]string{"NAME", "TYPE", "COMMENT"})
}
table.SetAutoFormatHeaders(false)
table.SetCenterSeparator("")
table.SetColumnSeparator("")
table.SetRowSeparator("")
table.SetHeaderLine(false)
table.SetBorder(false)
table.SetNoWhiteSpace(true)
table.SetTablePadding("\t")
nlrep := strings.NewReplacer("\r\n", " ", "\n", " ", "\r", " ")

if len(patterns) == 0 {
for _, t := range s.Tables {
if long {
table.Append([]string{t.Name, t.Type, nlrep.Replace(t.Comment)})
} else {
table.Append([]string{t.Name})
}
}
table.Render()
return nil
}

matches := []*schema.Table{}
for _, t := range s.Tables {
for _, p := range patterns {
if wildcard.MatchSimple(p, t.Name) {
matches = append(matches, t)
}
}
}
if len(matches) == 0 {
return fmt.Errorf("not found: %v", patterns)
}
for _, t := range matches {
for _, c := range t.Columns {
if long {
table.Append([]string{fmt.Sprintf("%s.%s", t.Name, c.Name), c.Type, nlrep.Replace(c.Comment)})
} else {
table.Append([]string{fmt.Sprintf("%s.%s", t.Name, c.Name)})
}
}
}
table.Render()
return nil
},
}

func loadLsArgs(args []string) ([]config.Option, []string, error) {
pattern := args
options := []config.Option{}
options = append(options, config.Include(append(tables, includes...)))
options = append(options, config.Exclude(excludes))
options = append(options, config.IncludeLabels(labels))
options = append(options, config.Distance(distance))
return options, pattern, nil
}

func init() {
rootCmd.AddCommand(lsCmd)
lsCmd.Flags().StringVarP(&configPath, "config", "c", "", "config file path")
lsCmd.Flags().StringSliceVarP(&tables, "table", "", []string{}, "target table (tables to include)")
lsCmd.Flags().StringSliceVarP(&includes, "include", "", []string{}, "tables to include")
lsCmd.Flags().StringSliceVarP(&excludes, "exclude", "", []string{}, "tables to exclude")
lsCmd.Flags().StringSliceVarP(&labels, "label", "", []string{}, "table labels to be included")
lsCmd.Flags().IntVarP(&distance, "distance", "", 0, "distance between related tables to be displayed")
lsCmd.Flags().BoolVarP(&long, "long", "l", false, "list in the long format")
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/mattn/go-sqlite3 v1.14.16
github.com/microsoft/go-mssqldb v0.19.0
github.com/minio/pkg v1.5.8
github.com/olekukonko/tablewriter v0.0.5
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2
github.com/snowflakedb/gosnowflake v1.6.16
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
Expand All @@ -315,6 +316,8 @@ github.com/mtibben/percent v0.2.1/go.mod h1:KG9uO+SZkUp+VkRHsCdYQV3XSZrrSpR3O9ib
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 h1:BvoENQQU+fZ9uukda/RzCAL/191HHwJA5b13R6diVlY=
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI=
github.com/pierrec/lz4/v4 v4.1.8/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
Expand Down

0 comments on commit ecc24d0

Please # to comment.