-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (50 loc) · 1.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"context"
"os"
gcs "cloud.google.com/go/storage"
"github.com/mauri870/gcsfs"
"github.com/spf13/cobra"
"google.golang.org/api/option"
)
type contextKey string
var (
GCSFS *gcsfs.FS
contextFSKey contextKey = "fs"
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "gcsfs",
Short: "io/fs.FS interface to GCS",
Long: "Interacts with files inside a Google Storage Bucket using Golang's io/fs.FS",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
bucket, err := cmd.Flags().GetString("bucket")
if err != nil {
return err
}
var opts []option.ClientOption
if cmd.Flags().Changed("without-authentication") {
opts = append(opts, option.WithoutAuthentication())
}
gcsClient, err := gcs.NewClient(cmd.Context(), opts...)
if err != nil {
return err
}
fsys := gcsfs.NewWithClient(gcsClient, bucket)
ctx := context.WithValue(cmd.Context(), contextFSKey, fsys)
cmd.SetContext(ctx)
return nil
},
}
func init() {
rootCmd.PersistentFlags().StringP("bucket", "b", "", "Bucket name to use")
rootCmd.MarkFlagRequired("bucket")
rootCmd.PersistentFlags().BoolP("without-authentication", "w", false, "Disables authentication. Useful to access public buckets")
}
func main() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}