File: main.go

package info (click to toggle)
winrmcp 0.0~git20170607.0.078cc0a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 148 kB
  • ctags: 55
  • sloc: makefile: 5
file content (104 lines) | stat: -rw-r--r-- 2,811 bytes parent folder | download | duplicates (3)
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
package main

import (
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"time"

	"github.com/packer-community/winrmcp/winrmcp"
)

var usage = `
Usage: winrmcp [options] [-help | <from> <to>]

  Copy a local file or directory to a remote directory.

Options:

  -user                   Name of the user to authenticate as
  -pass                   Password to authenticate with
  -addr=localhost:5985    Host and port of the remote machine
  -https                  Use HTTPS in preference to HTTP
  -insecure               Do not validate the HTTPS certificate chain
  -cacert                 Filename of CA cert to validate against
  -tlsservername          Server name to validate against when using https
  -op-timeout=60s         Timeout duration of each WinRM operation
  -max-ops-per-shell=15   Max number of operations per WinRM shell

`

func main() {
	if hasSwitch("-help") {
		fmt.Print(usage)
		os.Exit(0)
	}
	if err := runMain(); err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
}

func runMain() error {
	flags := flag.NewFlagSet("cli", flag.ContinueOnError)
	flags.Usage = func() { fmt.Print(usage) }
	addr := flags.String("addr", "localhost:5985", "winrm remote host:port")
	user := flags.String("user", "", "winrm admin username")
	pass := flags.String("pass", "", "winrm admin password")
	https := flags.Bool("https", false, "use https instead of http")
	insecure := flags.Bool("insecure", false, "do not validate https certificate chain")
	tlsservername := flags.String("tlsservername", "", "server name to validate against when using https")
	cacert := flags.String("cacert", "", "ca certificate to validate against")
	opTimeout := flags.Duration("op-timeout", time.Second*60, "operation timeout")
	maxOpsPerShell := flags.Int("max-ops-per-shell", 15, "max operations per shell")
	flags.Parse(os.Args[1:])

	var certBytes []byte
	var err error
	if *cacert != "" {
		certBytes, err = ioutil.ReadFile(*cacert)
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
	} else {
		certBytes = nil
	}

	client, err := winrmcp.New(*addr, &winrmcp.Config{
		Auth:                  winrmcp.Auth{User: *user, Password: *pass},
		Https:                 *https,
		Insecure:              *insecure,
		TLSServerName:         *tlsservername,
		CACertBytes:           certBytes,
		OperationTimeout:      *opTimeout,
		MaxOperationsPerShell: *maxOpsPerShell,
	})
	if err != nil {
		return err
	}

	args := flags.Args()
	if len(args) < 1 {
		return errors.New("Source directory is required.")
	}
	if len(args) < 2 {
		return errors.New("Remote directory is required.")
	}
	if len(args) > 2 {
		return errors.New("Too many arguments.")
	}

	return client.Copy(args[0], args[1])
}

func hasSwitch(name string) bool {
	for _, arg := range os.Args[1:] {
		if arg == name {
			return true
		}
	}
	return false
}