-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathplugin.go
803 lines (662 loc) · 20.4 KB
/
plugin.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
)
var ansibleFolder = "/etc/ansible"
var ansibleConfig = "/etc/ansible/ansible.cfg"
// var ansibleContent = `
// [defaults]
// host_key_checking = False
// `
const (
ModePlaybook = "playbook"
ModeAdhoc = "adhoc"
ModeVault = "vault"
)
// Constants for valid actions
const (
ActionEncrypt = "encrypt"
ActionDecrypt = "decrypt"
ActionEncryptString = "encrypt_string"
ActionView = "view"
ActionEdit = "edit"
ActionRekey = "rekey"
)
type (
Config struct {
Mode string
Requirements string
Galaxy string
Inventories []string
Playbooks []string
Limit string
SkipTags string
StartAtTask string
Tags string
ExtraVars []string
ModulePath []string
GalaxyForce bool
Check bool
Diff bool
FlushCache bool
ForceHandlers bool
ListHosts bool
ListTags bool
ListTasks bool
SyntaxCheck bool
Forks int
VaultID string
VaultPassword string
VaultPasswordFile string
Verbose int
PrivateKey string
PrivateKeyFile string
User string
Connection string
Timeout int
SSHCommonArgs string
SFTPExtraArgs string
SCPExtraArgs string
SSHExtraArgs string
Become bool
BecomeMethod string
BecomeUser string
DisableHostKeyChecking bool // Disable SSH host key checking
HostKeyChecking bool // Enable SSH host key validation
Installation string // Path to the Ansible executable or installation
InventoryContent string // Inline inventory content
Sudo bool // Use sudo for operations
SudoUser string // Sudo user for operations
VaultTmpPath string // Temporary path for vault password files and others
// Ad-Hoc Parameters
Hosts string // Target hosts for ad-hoc command
Module string // Module name for ad-hoc command
ModuleArguments string // Module arguments for ad-hoc command
DynamicInventory bool // Enable dynamic inventory
Extras string // Additional options for ad-hoc execution
VaultCredentialsKey string // Vault credentials ID for encrypted files (optional)
// Inventory string
// Vault Parameters
Action string // Action for vault operation (e.g., encrypt, decrypt)
Content string // Content for vault operation
Input string // Input file for vault operation
NewVaultCredentialsKey string // New vault credentials ID for rekeying
Output string // Output file for vault operation
}
Plugin struct {
Config Config
}
)
func (p *Plugin) Exec() error {
switch p.Config.Mode {
case ModePlaybook:
return p.executePlaybook()
case ModeAdhoc:
return p.executeAdhoc()
case ModeVault:
return p.executeVault()
default:
return errors.New("invalid mode: specify 'playbook' or 'adhoc'")
}
}
func (p *Plugin) executePlaybook() error {
if err := p.playbooks(); err != nil {
return err
}
if err := p.ansibleConfig(); err != nil {
return err
}
if p.Config.PrivateKey != "" {
if err := p.privateKey(); err != nil {
return err
}
defer os.Remove(p.Config.PrivateKeyFile)
}
if p.Config.VaultPassword != "" {
if err := p.vaultPass(); err != nil {
return err
}
defer os.Remove(p.Config.VaultPasswordFile)
}
// Handle inline inventory content
if err := p.setupInventory(); err != nil {
return err
}
// Validate custom Ansible installation
if err := p.validateInstallation(); err != nil {
return err
}
commands := []*exec.Cmd{
p.versionCommand(),
}
if p.Config.Requirements != "" {
commands = append(commands, p.requirementsCommand())
}
if p.Config.Galaxy != "" {
commands = append(commands, p.galaxyCommand())
}
for _, inventory := range p.Config.Inventories {
commands = append(commands, p.ansibleCommand(inventory))
}
for _, cmd := range commands {
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "ANSIBLE_FORCE_COLOR=1")
trace(cmd)
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}
// executeAdhoc executes the Ansible Ad-Hoc command
func (p *Plugin) executeAdhoc() error {
// Step 1: Validate required parameters
if p.Config.Hosts == "" {
return errors.New("hosts parameter is required for ad-hoc execution")
}
// Step 2: Default to 'command' module if no module is provided
module := p.Config.Module
if module == "" {
module = "command"
}
// Step 3: Build arguments for the ad-hoc command
args := []string{
p.Config.Hosts, // Target hosts
"-m", module, // Module to execute
}
// Step 4: Add module arguments
if p.Config.ModuleArguments != "" {
args = append(args, "-a", p.Config.ModuleArguments)
}
// Step 5: Add inventory files or inline content
if len(p.Config.Inventories) > 0 {
for _, inventory := range p.Config.Inventories {
args = append(args, "--inventory", inventory)
}
}
if p.Config.InventoryContent != "" {
tmpfile, err := os.CreateTemp("", "inventory")
if err != nil {
return fmt.Errorf("failed to create temporary inventory file: %w", err)
}
defer os.Remove(tmpfile.Name())
if _, err := tmpfile.WriteString(p.Config.InventoryContent); err != nil {
return fmt.Errorf("failed to write inventory content to temporary file: %w", err)
}
args = append(args, "--inventory", tmpfile.Name())
}
// Step 6: Handle privilege escalation
if p.Config.Become {
args = append(args, "--become")
}
if p.Config.BecomeUser != "" {
args = append(args, "--become-user", p.Config.BecomeUser)
}
// Step 7: Handle dynamic inventory
if p.Config.DynamicInventory {
args = append(args, "--dynamic-inventory")
}
// Step 8: Add extra variables
for _, ev := range p.Config.ExtraVars {
args = append(args, "--extra-vars", ev)
}
// Step 9: Add additional options
if p.Config.Extras != "" {
args = append(args, p.Config.Extras)
}
// Step 10: Handle forks for parallelism
if p.Config.Forks > 0 {
args = append(args, "--forks", strconv.Itoa(p.Config.Forks))
}
// Step 11: Handle host key checking
env := os.Environ()
if !p.Config.HostKeyChecking {
env = append(env, "ANSIBLE_HOST_KEY_CHECKING=False")
} else {
env = append(env, "ANSIBLE_HOST_KEY_CHECKING=True")
}
// Step 12: Handle vault credentials key
if p.Config.VaultCredentialsKey != "" {
tmpVaultFile, err := os.CreateTemp("", "vault-pass")
if err != nil {
return fmt.Errorf("failed to create temporary vault password file: %w", err)
}
defer os.Remove(tmpVaultFile.Name())
if _, err := tmpVaultFile.WriteString(p.Config.VaultCredentialsKey); err != nil {
return fmt.Errorf("failed to write vault password to temporary file: %w", err)
}
args = append(args, "--vault-password-file", tmpVaultFile.Name())
}
// Step 13: Handle vault temporary path
if p.Config.VaultTmpPath != "" {
args = append(args, "--vault-password-file", p.Config.VaultTmpPath)
}
// Step 14: Handle private key file
if p.Config.PrivateKeyFile != "" {
args = append(args, "--private-key", p.Config.PrivateKeyFile)
}
// Step 15: Use custom Ansible installation if provided
executable := "ansible"
if p.Config.Installation != "" {
executable = p.Config.Installation
}
// Step 16: Construct and execute the command
cmd := exec.Command(executable, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env // Pass environment variables
// Log the command for debugging purposes
fmt.Printf("Executing command: %s %v\n", executable, args)
// Step 17: Run the command
return cmd.Run()
}
// executeVault executes the Ansible Vault operation
func (p *Plugin) executeVault() error {
// Step 1: Validate the action
if err := validateAction(p.Config.Action); err != nil {
return err
}
// Step 2: Determine the ansible-vault executable path
vaultExecutable := "ansible-vault"
if p.Config.Installation != "" {
vaultExecutable = p.Config.Installation
}
// Step 3: Build arguments for the ansible-vault command
args := []string{p.Config.Action}
// Step 4: Handle input or content based on the action
if p.Config.Action == ActionEncryptString {
if p.Config.Content == "" {
return errors.New("content is required for encrypt_string action")
}
args = append(args, "--output", p.Config.Output)
} else {
if p.Config.Input == "" {
return fmt.Errorf("input file is required for %s action", p.Config.Action)
}
args = append(args, p.Config.Input)
}
// Step 5: Add output file (if applicable)
handleOutputFile(p.Config.Output, &args)
// Step 6: Handle vault password key
vaultPasswordFile, err := handleVaultPassword(p.Config.VaultCredentialsKey, p.Config.VaultTmpPath, &args)
if err != nil {
return err
}
defer os.Remove(vaultPasswordFile) // Clean up the temporary password file after execution
// Step 7: Handle new vault password key for rekeying
if p.Config.Action == ActionRekey && p.Config.NewVaultCredentialsKey != "" {
newVaultPasswordFile, err := handleNewVaultPassword(p.Config.NewVaultCredentialsKey, p.Config.VaultTmpPath, &args)
if err != nil {
return err
}
defer os.Remove(newVaultPasswordFile) // Cleanup after execution
}
// Step 8: Construct the command
cmd := exec.Command(vaultExecutable, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
// Step 9: Pass the string content via stdin if encrypt_string
if p.Config.Action == ActionEncryptString {
stdin, err := cmd.StdinPipe()
if err != nil {
return fmt.Errorf("failed to open stdin pipe: %w", err)
}
go func() {
defer stdin.Close()
if _, writeErr := stdin.Write([]byte(p.Config.Content)); writeErr != nil {
fmt.Fprintf(os.Stderr, "failed to write to stdin: %v\n", writeErr)
}
}()
}
// Step 10: Log the command for debugging purposes
fmt.Printf("Executing command: %s %v\n", vaultExecutable, args)
// Step 11: Execute the command
if err := cmd.Run(); err != nil {
return fmt.Errorf("ansible-vault command failed: %w", err)
}
return nil
}
// validateAction validates the provided action
func validateAction(action string) error {
validActions := map[string]bool{
ActionEncrypt: true,
ActionDecrypt: true,
ActionEncryptString: true,
ActionView: true,
ActionEdit: true,
ActionRekey: true,
}
if !validActions[action] {
return fmt.Errorf("invalid action: %s. Supported actions: encrypt, decrypt, encrypt_string, view, edit, rekey", action)
}
return nil
}
// handleOutputFile adds the output file flag if provided
func handleOutputFile(output string, args *[]string) {
if output != "" {
*args = append(*args, "--output", output)
}
}
// handleVaultPassword writes the vault password to a temporary file and appends it to args
func handleVaultPassword(vaultKey string, vaultTmpPath string, args *[]string) (string, error) {
if vaultKey == "" {
return "", errors.New("vaultCredentialsKey is required for vault operations")
}
// Create a temporary Vault password file
tmpVaultFile, err := createVaultTmpFile(vaultTmpPath, "vault-pass")
if err != nil {
return "", fmt.Errorf("failed to create temporary vault password file: %w", err)
}
// Write the password to the file
if _, err := tmpVaultFile.WriteString(vaultKey); err != nil {
tmpVaultFile.Close()
return "", fmt.Errorf("failed to write vault key to temporary file: %w", err)
}
// Close the file to ensure the data is flushed
if err := tmpVaultFile.Close(); err != nil {
return "", fmt.Errorf("failed to close vault password file: %w", err)
}
// Set correct permissions
if err := os.Chmod(tmpVaultFile.Name(), 0600); err != nil {
return "", fmt.Errorf("failed to set permissions on vault password file: %w", err)
}
// Append the file path to the args
*args = append(*args, "--vault-password-file", tmpVaultFile.Name())
return tmpVaultFile.Name(), nil
}
func handleNewVaultPassword(newVaultKey string, vaultTmpPath string, args *[]string) (string, error) {
// Create a temporary file for the new vault password
tmpNewVaultFile, err := createVaultTmpFile(vaultTmpPath, "new-vault-pass")
if err != nil {
return "", fmt.Errorf("failed to create temporary new vault password file: %w", err)
}
// Write the new password to the file
if _, err := tmpNewVaultFile.WriteString(newVaultKey); err != nil {
tmpNewVaultFile.Close()
return "", fmt.Errorf("failed to write new vault key to temporary file: %w", err)
}
// Close the file to ensure the data is flushed
if err := tmpNewVaultFile.Close(); err != nil {
return "", fmt.Errorf("failed to close new vault password file: %w", err)
}
// Append the new vault password file to the args
*args = append(*args, "--new-vault-password-file", tmpNewVaultFile.Name())
return tmpNewVaultFile.Name(), nil
}
// createVaultTmpFile creates a temporary file in the specified VaultTmpPath or system default
func createVaultTmpFile(vaultTmpPath, prefix string) (*os.File, error) {
if vaultTmpPath != "" {
if err := ensureDirectoryExists(vaultTmpPath); err != nil {
return nil, err
}
return os.CreateTemp(vaultTmpPath, prefix)
}
return os.CreateTemp("", prefix)
}
// ensureDirectoryExists ensures the directory exists or creates it
func ensureDirectoryExists(dir string) error {
info, err := os.Stat(dir)
if os.IsNotExist(err) {
return os.MkdirAll(dir, 0755)
}
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("%s exists but is not a directory", dir)
}
return nil
}
func (p *Plugin) ansibleConfig() error {
if err := os.MkdirAll(ansibleFolder, os.ModePerm); err != nil {
return errors.Wrap(err, "failed to create ansible directory")
}
ansibleConfigContent := "[defaults]\n"
if p.Config.DisableHostKeyChecking {
ansibleConfigContent += "host_key_checking = False\n"
}
if err := os.WriteFile(ansibleConfig, []byte(ansibleConfigContent), 0600); err != nil {
return errors.Wrap(err, "failed to create ansible config")
}
return nil
}
func (p *Plugin) privateKey() error {
tmpfile, err := os.CreateTemp("", "privateKey")
if err != nil {
return errors.Wrap(err, "failed to create private key file")
}
if _, err := tmpfile.Write([]byte(p.Config.PrivateKey)); err != nil {
return errors.Wrap(err, "failed to write private key file")
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close private key file")
}
p.Config.PrivateKeyFile = tmpfile.Name()
return nil
}
func (p *Plugin) vaultPass() error {
tmpfile, err := os.CreateTemp("", "vaultPass")
if err != nil {
return errors.Wrap(err, "failed to create vault password file")
}
if _, err := tmpfile.Write([]byte(p.Config.VaultPassword)); err != nil {
return errors.Wrap(err, "failed to write vault password file")
}
if err := tmpfile.Close(); err != nil {
return errors.Wrap(err, "failed to close vault password file")
}
p.Config.VaultPasswordFile = tmpfile.Name()
return nil
}
// setupInventory handles inline inventory content
func (p *Plugin) setupInventory() error {
if p.Config.InventoryContent != "" {
tmpfile, err := os.CreateTemp("", "inventory")
if err != nil {
return errors.Wrap(err, "failed to create temporary inventory file")
}
if _, err := tmpfile.WriteString(p.Config.InventoryContent); err != nil {
return errors.Wrap(err, "failed to write inventory content")
}
defer tmpfile.Close()
p.Config.Inventories = append(p.Config.Inventories, tmpfile.Name())
}
return nil
}
// validateInstallation checks if the specified Ansible installation exists
func (p *Plugin) validateInstallation() error {
if p.Config.Installation != "" {
if _, err := exec.LookPath(p.Config.Installation); err != nil {
return errors.Wrapf(err, "specified Ansible installation not found: %s", p.Config.Installation)
}
}
return nil
}
func (p *Plugin) playbooks() error {
var (
playbooks []string
)
for _, p := range p.Config.Playbooks {
files, err := filepath.Glob(p)
if err != nil {
playbooks = append(playbooks, p)
continue
}
playbooks = append(playbooks, files...)
}
if len(playbooks) == 0 {
return errors.New("failed to find playbook files")
}
p.Config.Playbooks = playbooks
return nil
}
func (p *Plugin) versionCommand() *exec.Cmd {
args := []string{
"--version",
}
return exec.Command(
"ansible",
args...,
)
}
func (p *Plugin) requirementsCommand() *exec.Cmd {
args := []string{
"install",
"--upgrade",
"--requirement",
p.Config.Requirements,
}
return exec.Command(
"pip",
args...,
)
}
func (p *Plugin) galaxyCommand() *exec.Cmd {
args := []string{
"install",
}
if p.Config.GalaxyForce {
args = append(args, "--force")
}
args = append(args,
"--role-file",
p.Config.Galaxy,
)
if p.Config.Verbose > 0 {
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose)))
}
return exec.Command(
"ansible-galaxy",
args...,
)
}
func (p *Plugin) ansibleCommand(inventory string) *exec.Cmd {
args := []string{
"--inventory",
inventory,
}
if len(p.Config.ModulePath) > 0 {
args = append(args, "--module-path", strings.Join(p.Config.ModulePath, ":"))
}
if p.Config.VaultID != "" {
args = append(args, "--vault-id", p.Config.VaultID)
}
if p.Config.VaultPasswordFile != "" {
args = append(args, "--vault-password-file", p.Config.VaultPasswordFile)
}
if p.Config.VaultTmpPath != "" {
args = append(args, "--vault-password-file", p.Config.VaultTmpPath) // Vault temporary path
}
for _, v := range p.Config.ExtraVars {
args = append(args, "--extra-vars", v)
}
if p.Config.ListHosts {
args = append(args, "--list-hosts")
args = append(args, p.Config.Playbooks...)
return exec.Command(
"ansible-playbook",
args...,
)
}
if p.Config.SyntaxCheck {
args = append(args, "--syntax-check")
args = append(args, p.Config.Playbooks...)
return exec.Command(
"ansible-playbook",
args...,
)
}
if p.Config.Check {
args = append(args, "--check")
}
if p.Config.Diff {
args = append(args, "--diff")
}
if p.Config.FlushCache {
args = append(args, "--flush-cache")
}
if p.Config.ForceHandlers {
args = append(args, "--force-handlers")
}
if p.Config.Forks != 5 {
args = append(args, "--forks", strconv.Itoa(p.Config.Forks))
}
if p.Config.Limit != "" {
args = append(args, "--limit", p.Config.Limit)
}
if p.Config.ListTags {
args = append(args, "--list-tags")
}
if p.Config.ListTasks {
args = append(args, "--list-tasks")
}
if p.Config.SkipTags != "" {
args = append(args, "--skip-tags", p.Config.SkipTags)
}
if p.Config.StartAtTask != "" {
args = append(args, "--start-at-task", p.Config.StartAtTask)
}
if p.Config.Tags != "" {
args = append(args, "--tags", p.Config.Tags)
}
if p.Config.PrivateKeyFile != "" {
args = append(args, "--private-key", p.Config.PrivateKeyFile)
}
if p.Config.User != "" {
args = append(args, "--user", p.Config.User)
}
if p.Config.Connection != "" {
args = append(args, "--connection", p.Config.Connection)
}
if p.Config.Timeout != 0 {
args = append(args, "--timeout", strconv.Itoa(p.Config.Timeout))
}
if p.Config.SSHCommonArgs != "" {
args = append(args, "--ssh-common-args", p.Config.SSHCommonArgs)
}
if p.Config.SFTPExtraArgs != "" {
args = append(args, "--sftp-extra-args", p.Config.SFTPExtraArgs)
}
if p.Config.SCPExtraArgs != "" {
args = append(args, "--scp-extra-args", p.Config.SCPExtraArgs)
}
if p.Config.SSHExtraArgs != "" {
args = append(args, "--ssh-extra-args", p.Config.SSHExtraArgs)
}
if p.Config.Become {
args = append(args, "--become")
}
if p.Config.BecomeMethod != "" {
args = append(args, "--become-method", p.Config.BecomeMethod)
}
if p.Config.BecomeUser != "" {
args = append(args, "--become-user", p.Config.BecomeUser)
}
if p.Config.Verbose > 0 {
args = append(args, fmt.Sprintf("-%s", strings.Repeat("v", p.Config.Verbose)))
}
args = append(args, p.Config.Playbooks...)
return exec.Command(p.ansibleExecutable(), args...)
// return exec.Command(
// "ansible-playbook",
// args...,
// )
}
// ansibleExecutable determines the executable to use
func (p *Plugin) ansibleExecutable() string {
if p.Config.Installation != "" {
return p.Config.Installation
}
return "ansible-playbook"
}
func trace(cmd *exec.Cmd) {
fmt.Println("$", strings.Join(cmd.Args, " "))
}