/
put.go
144 lines (132 loc) · 3.79 KB
/
put.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Copyright 2022 The Ent Authors.
//
// Licensed 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 cmd
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"github.com/fatih/color"
"github.com/google/ent/cmd/ent/config"
"github.com/google/ent/cmd/ent/remote"
"github.com/google/ent/log"
"github.com/google/ent/nodeservice"
"github.com/google/ent/utils"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
"github.com/spf13/cobra"
"github.com/tonistiigi/units"
)
var (
remoteFlag string
digestFormatFlag string
porcelainFlag bool
)
var putCmd = &cobra.Command{
Use: "put [filename]",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filename := ""
if len(args) > 0 {
filename = args[0]
}
ctx := context.Background()
if filename == "" {
err := putStdin()
if err != nil {
log.Criticalf(ctx, "could not read from stdin: %v", err)
os.Exit(1)
}
} else {
_, err := traverseFileOrDir(filename, put)
if err != nil {
log.Criticalf(ctx, "could not traverse file: %v", err)
os.Exit(1)
}
}
},
}
func putStdin() error {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("could not read stdin: %v", err)
}
digest := utils.ComputeDigest(data)
link := cid.NewCidV1(utils.TypeRaw, multihash.Multihash(digest))
return put(data, link, "-")
}
// func putFile(filename string) error {
// data, err := os.ReadFile(filename)
// if err != nil {
// return fmt.Errorf("could not read file %q: %v", filename, err)
// }
// return putData(data)
// }
func put(b []byte, link cid.Cid, name string) error {
ctx := context.Background()
config := config.ReadConfig()
r := config.Remotes[0]
if remoteFlag != "" {
var err error
r, err = remote.GetRemote(config, remoteFlag)
if err != nil {
return fmt.Errorf("could not use remote: %v", err)
}
}
nodeService := remote.GetObjectStore(r)
size := len(b)
switch link.Type() {
case utils.TypeRaw:
digest := utils.Digest(link.Hash())
digestString := utils.FormatDigest(digest, digestFormatFlag)
marker := color.GreenString("-")
if exists(nodeService, digest) {
marker = color.GreenString("✓")
} else {
log.Infof(ctx, "putting object %q", digestString)
_, err := nodeService.Put(ctx, uint64(size), bytes.NewReader(b))
if err != nil {
log.Errorf(ctx, "could not put object: %v", err)
return fmt.Errorf("could not put object: %v", err)
}
marker = color.BlueString("↑")
}
if porcelainFlag {
fmt.Printf("%s\n", digestString)
} else {
fmt.Printf("%s [%s %s] %s %.0f\n", color.YellowString(digestString), marker, r.Name, name, units.Bytes(size))
}
return nil
case utils.TypeDAG:
return nil
default:
return fmt.Errorf("unknown type: %v", link.Type())
}
}
func exists(nodeService nodeservice.ObjectGetter, digest utils.Digest) bool {
ctx := context.Background()
ok, err := nodeService.Has(ctx, digest)
if err != nil {
log.Errorf(ctx, "could not check existence of %q: %v", digest, err)
return false
}
return ok
}
func init() {
putCmd.PersistentFlags().StringVar(&remoteFlag, "remote", "", "remote")
putCmd.PersistentFlags().StringVar(&digestFormatFlag, "digest-format", "b58", "format [human, hex, b58]")
putCmd.PersistentFlags().BoolVar(&porcelainFlag, "porcelain", false, "porcelain output (parseable by machines)")
}