Debian Codegraph @

cli/utils.go

raw symbols SCIP
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package cli
15
16 import (
17 "fmt"
18 "os"
19 "sort"
20 "strings"
21
22 "github.com/notaryproject/notation-plugin-framework-go/internal/slices"
23 "github.com/notaryproject/notation-plugin-framework-go/plugin"
24 )
25
26 func getValidArgsString(md *plugin.GetMetadataResponse) string {
27 return fmt.Sprintf(`<%s>`, strings.Join(getValidArgs(md), "|"))
28 }
29
30 // getValidArgs returns list of valid arguments depending upon the plugin capabilities
31 func getValidArgs(md *plugin.GetMetadataResponse) []string {
32 args := []string{
33 string(plugin.CommandGetMetadata), string(plugin.Version),
34 }
35
36 if slices.Contains(md.Capabilities, plugin.CapabilitySignatureGenerator) {
37 args = append(args, string(plugin.CommandGenerateSignature), string(plugin.CommandDescribeKey))
38 }
39
40 if slices.Contains(md.Capabilities, plugin.CapabilityEnvelopeGenerator) {
41 args = append(args, string(plugin.CommandGenerateEnvelope))
42 }
43
44 if slices.Contains(md.Capabilities, plugin.CapabilityTrustedIdentityVerifier) || slices.Contains(md.Capabilities, plugin.CapabilityRevocationCheckVerifier) {
45 args = append(args, string(plugin.CommandVerifySignature))
46 }
47
48 sort.Strings(args)
49 return args
50 }
51
52 // deliverError print to standard error and then return nonzero exit code
53 func deliverError(message string) {
54 _, _ = fmt.Fprint(os.Stderr, message)
55 os.Exit(1)
56 }
57
58 // deferStdout is used to make sure that nothing get emitted to stdout and stderr until intentionally rescued.
59 // This is required to make sure that the plugin or its dependency doesn't interfere with notation <-> plugin communication
60 func deferStdout() func() {
61 // Ignoring error because we don't want plugin to fail if `os.DevNull` is misconfigured.
62 null, _ := os.Open(os.DevNull)
63 sout := os.Stdout
64 serr := os.Stderr
65 os.Stdout = null
66 os.Stderr = null
67
68 return func() {
69 err := null.Close()
70 if err != nil {
71 return
72 }
73 os.Stdout = sout
74 os.Stderr = serr
75 }
76 }
77
78 // discardLogger implements Logger but logs nothing. It is used when user
79 // disenabled logging option in notation, i.e. loggerKey is not in the context.
80 type discardLogger struct{}
81
82 func (dl *discardLogger) Debug(_ ...interface{}) {
83 }
84
85 func (dl *discardLogger) Debugf(_ string, _ ...interface{}) {
86 }
87
88 func (dl *discardLogger) Debugln(_ ...interface{}) {
89 }
90
91 func (dl *discardLogger) Info(_ ...interface{}) {
92 }
93
94 func (dl *discardLogger) Infof(_ string, _ ...interface{}) {
95 }
96
97 func (dl *discardLogger) Infoln(_ ...interface{}) {
98 }
99
100 func (dl *discardLogger) Warn(_ ...interface{}) {
101 }
102
103 func (dl *discardLogger) Warnf(_ string, _ ...interface{}) {
104 }
105
106 func (dl *discardLogger) Warnln(_ ...interface{}) {
107 }
108
109 func (dl *discardLogger) Error(_ ...interface{}) {
110 }
111
112 func (dl *discardLogger) Errorf(_ string, _ ...interface{}) {
113 }
114
115 func (dl *discardLogger) Errorln(_ ...interface{}) {
116 }