update.go 2.24 KB
Newer Older
1
2
3
package cmd

import (
4
	"io/ioutil"
5
6
7
	"path/filepath"

	"github.com/go-errors/errors"
8
	irma "github.com/privacybydesign/irmago"
9
	"github.com/privacybydesign/irmago/internal/common"
10
	"github.com/spf13/cobra"
11
12
13
)

var updateCmd = &cobra.Command{
14
	Use:   "update [<path>...]",
15
16
	Short: "Update a scheme",
	Long:  updateHelp(),
17
	Run: func(cmd *cobra.Command, args []string) {
18
		var paths []string
19
		irmaconf := irma.DefaultSchemesPath()
20
21
22
23
		if len(args) != 0 {
			paths = args
		} else {
			if irmaconf == "" {
24
				die("Failed to find default irma_configuration path", nil)
25
26
27
28
29
30
31
32
33
34
35
			}
			files, err := ioutil.ReadDir(irmaconf)
			if err != nil {
				die("Failed to read default irma_configuration path", err)
			}
			paths = make([]string, 0, len(files))
			for _, file := range files {
				if file.IsDir() {
					paths = append(paths, filepath.Join(irmaconf, file.Name()))
				}
			}
36
		}
37

38
39
40
41
		if err := updateSchemeManager(paths); err != nil {
			die("Updating schemes failed", err)
		}
	},
42
43
44
45
46
}

func updateSchemeManager(paths []string) error {
	// Before doing anything, first check that all paths are scheme managers
	for _, path := range paths {
47
48
49
50
51
52
		isscheme, err := common.IsScheme(path, true)
		if err != nil {
			return err
		}
		if !isscheme {
			return errors.Errorf("%s is not a scheme", path)
53
54
55
56
		}
	}

	for _, path := range paths {
57
		path, err := filepath.Abs(path)
Sietse Ringers's avatar
Sietse Ringers committed
58
59
60
		if err != nil {
			return err
		}
61
		conf, err := irma.NewConfiguration(filepath.Dir(path), irma.ConfigurationOptions{})
62
63
64
		if err != nil {
			return err
		}
65
66
		scheme, err := conf.ParseSchemeFolder(path)
		if err != nil {
67
68
			return err
		}
69
		if err = conf.UpdateScheme(scheme, nil); err != nil {
70
71
72
73
74
75
			return err
		}
	}

	return nil
}
76
77

func updateHelp() string {
78
	defaultIrmaconf := irma.DefaultSchemesPath()
79
80
81
82
83
84
85
86
87
88
89
	str := "The update command updates an IRMA scheme within an irma_configuration folder by comparing its index with the online version, and downloading any new and changed files.\n\n"
	if defaultIrmaconf != "" {
		str += "If no paths are given, the default schemes at " + defaultIrmaconf + " are updated.\n\n"
	}
	str += "Careful: this command could fail and invalidate or destroy your scheme manager folder! Use this only if you can restore it from git or backups."
	return str
}

func init() {
	schemeCmd.AddCommand(updateCmd)
}