-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
71 lines (58 loc) · 1.77 KB
/
main.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
package main
// go install github.com/nyaruka/goflow/cmd/flowmigrate
// cat legacy_flow.json | flowmigrate
// cat legacy_export.json | jq '.flows[0]' | flowmigrate
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"github.com/Masterminds/semver"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/goflow/flows/definition"
"github.com/nyaruka/goflow/flows/definition/migrations"
)
func main() {
var toVersion, baseMediaURL string
var pretty bool
flags := flag.NewFlagSet("", flag.ExitOnError)
flags.StringVar(&toVersion, "to", definition.CurrentSpecVersion.String(), "Target flow spec version")
flags.StringVar(&baseMediaURL, "base-media-url", "", "Base URL for media files")
flags.BoolVar(&pretty, "pretty", false, "Pretty format output")
flags.Parse(os.Args[1:])
reader := bufio.NewReader(os.Stdin)
output, err := Migrate(reader, semver.MustParse(toVersion), baseMediaURL, pretty)
if err != nil {
fmt.Println(err)
} else {
fmt.Println(string(output))
}
}
// Migrate reads a flow definition as JSON and migrates it
func Migrate(reader io.Reader, toVersion *semver.Version, baseMediaURL string, pretty bool) ([]byte, error) {
data, err := io.ReadAll(reader)
if err != nil {
return nil, err
}
var mCfg *migrations.Config
if baseMediaURL != "" {
mCfg = &migrations.Config{BaseMediaURL: baseMediaURL}
}
migrated, err := migrations.MigrateToVersion(data, toVersion, mCfg)
if err != nil {
return nil, err
}
// if we've migrated to the engine version, validate the flow can be read by the engine
if toVersion == nil || toVersion.Equal(definition.CurrentSpecVersion) {
_, err = definition.ReadFlow(migrated, nil)
if err != nil {
return nil, err
}
}
if pretty {
return jsonx.MarshalPretty(json.RawMessage(migrated))
}
return migrated, nil
}