This repository has been archived by the owner on Oct 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
98 lines (92 loc) · 2.54 KB
/
metadata.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/gepis/strge"
"github.com/gepis/strge/pkg/mflag"
)
var metadataQuiet = false
func metadata(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
if len(args) < 1 {
return 1
}
metadataDict := make(map[string]string)
missingAny := false
for _, what := range args {
if metadata, err := m.Metadata(what); err == nil {
metadataDict[what] = strings.TrimSuffix(metadata, "\n")
} else {
missingAny = true
}
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(metadataDict)
} else {
for _, what := range args {
if metadataQuiet {
fmt.Printf("%s\n", metadataDict[what])
} else {
fmt.Printf("%s: %s\n", what, metadataDict[what])
}
}
}
if missingAny {
return 1
}
return 0
}
func setMetadata(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
if len(args) < 1 {
return 1
}
if paramMetadataFile == "" && paramMetadata == "" {
fmt.Fprintf(os.Stderr, "no new metadata provided\n")
return 1
}
if paramMetadataFile != "" {
f, err := os.Open(paramMetadataFile)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
b, err := ioutil.ReadAll(f)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 1
}
paramMetadata = string(b)
}
if err := m.SetMetadata(args[0], paramMetadata); err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
return 1
}
return 0
}
func init() {
commands = append(commands, command{
names: []string{"metadata"},
optionsHelp: "[LayerOrImageOrContainerNameOrID [...]]",
usage: "Retrieve layer, image, or container metadata",
minArgs: 1,
action: metadata,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&metadataQuiet, []string{"-quiet", "q"}, metadataQuiet, "Omit names and IDs")
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
})
commands = append(commands, command{
names: []string{"set-metadata", "setmetadata"},
optionsHelp: "[options [...]] layerOrImageOrContainerNameOrID",
usage: "Set layer, image, or container metadata",
minArgs: 1,
maxArgs: 1,
action: setMetadata,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.StringVar(¶mMetadata, []string{"-metadata", "m"}, "", "Metadata")
flags.StringVar(¶mMetadataFile, []string{"-metadata-file", "f"}, "", "Metadata File")
},
})
}