|
| 1 | +/* |
| 2 | +Copyright © 2022 Edgar Ramírez-Mondragón |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | +package cmd |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "log" |
| 23 | + "os" |
| 24 | + |
| 25 | + "github.com/spf13/cobra" |
| 26 | + "github.com/spf13/viper" |
| 27 | + "meltano.com/target-jsonl-blob/target" |
| 28 | + |
| 29 | + "gocloud.dev/blob" |
| 30 | + // _ "gocloud.dev/blob/gcsblob" |
| 31 | + _ "gocloud.dev/blob/fileblob" |
| 32 | + _ "gocloud.dev/blob/s3blob" |
| 33 | +) |
| 34 | + |
| 35 | +var ( |
| 36 | + configFile string |
| 37 | + inputFile string |
| 38 | + C target.Config |
| 39 | +) |
| 40 | + |
| 41 | +var rootCmd = &cobra.Command{ |
| 42 | + Use: "target-jsonl-blob", |
| 43 | + Short: "JSONLines Singer target for blob storages", |
| 44 | + Run: func(cmd *cobra.Command, args []string) {}, |
| 45 | +} |
| 46 | + |
| 47 | +// Execute adds all child commands to the root command and sets flags appropriately. |
| 48 | +// This is called by main.main(). It only needs to happen once to the rootCmd. |
| 49 | +func Execute() { |
| 50 | + err := rootCmd.Execute() |
| 51 | + if err != nil { |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + |
| 55 | + readConfig(configFile) |
| 56 | + |
| 57 | + if C.Bucket == "" { |
| 58 | + log.Fatalf("Bucket is required") |
| 59 | + } |
| 60 | + |
| 61 | + if C.KeyTemplate == "" { |
| 62 | + C.KeyTemplate = target.DEFAULT_KEY_TEMPLATE |
| 63 | + } |
| 64 | + |
| 65 | + ctx := context.Background() |
| 66 | + bucket, err := blob.OpenBucket(ctx, C.Bucket) |
| 67 | + if err != nil { |
| 68 | + log.Fatalf("Unable to open bucket, %v", err) |
| 69 | + } |
| 70 | + defer bucket.Close() |
| 71 | + |
| 72 | + var lines io.Reader |
| 73 | + |
| 74 | + if inputFile == "" { |
| 75 | + lines = os.Stdin |
| 76 | + } else { |
| 77 | + lines, err = os.Open(inputFile) |
| 78 | + if err != nil { |
| 79 | + fmt.Print(err) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + target.ProcessLines(lines, C, ctx, bucket) |
| 84 | + |
| 85 | + if inputFile == "" { |
| 86 | + target.ProcessLines(os.Stdin, C, ctx, bucket) |
| 87 | + } else { |
| 88 | + inputLines, err := os.Open(inputFile) |
| 89 | + if err != nil { |
| 90 | + fmt.Print(err) |
| 91 | + } |
| 92 | + target.ProcessLines(inputLines, C, ctx, bucket) |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +func init() { |
| 98 | + rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "", "Config file") |
| 99 | + rootCmd.PersistentFlags().StringVarP(&inputFile, "input", "i", "", "Input file") |
| 100 | + rootCmd.MarkPersistentFlagRequired("config") |
| 101 | +} |
| 102 | + |
| 103 | +// initConfig reads in config file and ENV variables if set. |
| 104 | +func readConfig(file string) { |
| 105 | + viper.SetConfigFile(file) |
| 106 | + |
| 107 | + if err := viper.ReadInConfig(); err == nil { |
| 108 | + log.Printf("Using config file %s", viper.ConfigFileUsed()) |
| 109 | + } else { |
| 110 | + fmt.Fprintln(os.Stderr, "Config file is not valid") |
| 111 | + os.Exit(1) |
| 112 | + } |
| 113 | + |
| 114 | + if err := viper.Unmarshal(&C); err != nil { |
| 115 | + log.Fatalf("Unable to decode into struct, %v", err) |
| 116 | + } |
| 117 | +} |
0 commit comments