|
| 1 | +// Copyright (C) 2021 CGI France |
| 2 | +// |
| 3 | +// This file is part of LINO. |
| 4 | +// |
| 5 | +// LINO is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU General Public License as published by |
| 7 | +// the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// LINO is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with LINO. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +package sequence |
| 19 | + |
| 20 | +import ( |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "github.com/cgi-fr/lino/internal/app/urlbuilder" |
| 25 | + "github.com/cgi-fr/lino/pkg/dataconnector" |
| 26 | + "github.com/cgi-fr/lino/pkg/sequence" |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +// newExtractCommand implements the cli relation extract command |
| 31 | +func newExtractCommand(fullName string, err *os.File, out *os.File, in *os.File) *cobra.Command { |
| 32 | + cmd := &cobra.Command{ |
| 33 | + Use: "extract [DB Alias Name]", |
| 34 | + Short: "Extract sequence name from database", |
| 35 | + Long: "", |
| 36 | + Example: fmt.Sprintf(" %[1]s sequence extract mydatabase", fullName), |
| 37 | + Args: cobra.ExactArgs(1), |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + alias, e1 := dataconnector.Get(dataconnectorStorage, args[0]) |
| 40 | + if e1 != nil { |
| 41 | + fmt.Fprintln(err, e1.Description) |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + if alias == nil { |
| 46 | + fmt.Fprintln(err, "no dataconnector named "+args[0]) |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + u := urlbuilder.BuildURL(alias, err) |
| 51 | + |
| 52 | + factory, ok := sequenceUpdatorFactories[u.Unaliased] |
| 53 | + if !ok { |
| 54 | + fmt.Fprintln(err, "no extractor found for database type") |
| 55 | + os.Exit(1) |
| 56 | + } |
| 57 | + |
| 58 | + extractor := factory.New(u.URL.String(), alias.Schema) |
| 59 | + |
| 60 | + tableTables, e2 := tableStorage.List() |
| 61 | + if e2 != nil { |
| 62 | + fmt.Fprintln(err, e2.Description) |
| 63 | + os.Exit(1) |
| 64 | + } |
| 65 | + |
| 66 | + tables := []sequence.Table{} |
| 67 | + for _, tbl := range tableTables { |
| 68 | + tables = append(tables, sequence.Table{Name: tbl.Name, Keys: tbl.Keys}) |
| 69 | + } |
| 70 | + |
| 71 | + e3 := sequence.Extract(extractor, tables, sequenceStorage) |
| 72 | + if e3 != nil { |
| 73 | + fmt.Fprintln(err, e2.Description) |
| 74 | + os.Exit(1) |
| 75 | + } |
| 76 | + }, |
| 77 | + } |
| 78 | + cmd.SetOut(out) |
| 79 | + cmd.SetErr(err) |
| 80 | + cmd.SetIn(in) |
| 81 | + return cmd |
| 82 | +} |
0 commit comments