MCPcopy
hub / github.com/getsops/sops / ExecWithFile

Function ExecWithFile

cmd/sops/subcommand/exec/exec.go:59–128  ·  view source on GitHub ↗
(opts ExecOpts)

Source from the content-addressed store, hash-verified

57}
58
59func ExecWithFile(opts ExecOpts) error {
60 var userEnv []string
61 if opts.User != "" {
62 userEnv = UserEnv(opts.User)
63 SwitchUser(opts.User)
64 }
65
66 if runtime.GOOS == "windows" && opts.Fifo {
67 log.Warn("no fifos on windows, use --no-fifo next time")
68 opts.Fifo = false
69 }
70
71 dir, err := os.MkdirTemp("", ".sops")
72 if err != nil {
73 return err
74 }
75 defer os.RemoveAll(dir)
76
77 if opts.Filename != "" {
78 if filepath.IsAbs(opts.Filename) || !filepath.IsLocal(opts.Filename) {
79 return fmt.Errorf("The provided filename is not a local path.")
80 }
81 }
82
83 var filename string
84 if opts.Fifo {
85 // fifo handling needs to be async, even opening to write
86 // will block if there is no reader present
87 filename = opts.Filename
88 if filename == "" {
89 filename = FallbackFilename
90 }
91 filename, err = GetPipe(dir, filename)
92 if err != nil {
93 return err
94 }
95 go WritePipe(filename, opts.Plaintext)
96 } else {
97 // GetFile handles opts.Filename == "" specially, that's why we have
98 // to pass in opts.Filename without handling the fallback here
99 handle, err := GetFile(dir, opts.Filename)
100 if err != nil {
101 return err
102 }
103 handle.Write(opts.Plaintext)
104 handle.Close()
105 filename = handle.Name()
106 }
107
108 var env []string
109 if !opts.Pristine {
110 env = os.Environ()
111 }
112 env = append(env, userEnv...)
113 env = append(env, opts.Env...)
114
115 placeholdered := strings.Replace(opts.Command, "{}", filename, -1)
116 cmd := BuildCommand(placeholdered)

Callers 3

mainFunction · 0.92

Calls 7

GetFileFunction · 0.85
UserEnvFunction · 0.70
SwitchUserFunction · 0.70
GetPipeFunction · 0.70
WritePipeFunction · 0.70
BuildCommandFunction · 0.70
NameMethod · 0.65