github.com/vchain-us/vcn@v0.9.11-0.20210921212052-a2484d23c0b3/pkg/cmd/sign/alert.go (about) 1 /* 2 * Copyright (c) 2018-2020 vChain, Inc. All Rights Reserved. 3 * This software is released under GPL3. 4 * The full license information can be found under: 5 * https://www.gnu.org/licenses/gpl-3.0.en.html 6 * 7 */ 8 9 package sign 10 11 import ( 12 "fmt" 13 "os" 14 "path/filepath" 15 "strings" 16 17 "github.com/vchain-us/vcn/pkg/store" 18 19 "github.com/vchain-us/vcn/pkg/extractor/dir" 20 "github.com/vchain-us/vcn/pkg/extractor/file" 21 "github.com/vchain-us/vcn/pkg/extractor/git" 22 "github.com/vchain-us/vcn/pkg/uri" 23 24 "github.com/vchain-us/vcn/pkg/api" 25 ) 26 27 type alertOptions struct { 28 arg string 29 name string 30 email string 31 } 32 33 func handleAlert(opts *alertOptions, u api.User, a api.Artifact, v api.BlockchainVerification, output string) error { 34 if opts == nil { 35 return nil 36 } 37 38 m := api.Metadata{} 39 40 // make path absolute 41 aURI, err := uri.Parse(opts.arg) 42 if err != nil { 43 return fmt.Errorf("invalid argument for alert: %s", opts.arg) 44 } 45 46 switch a.Kind { 47 case file.Scheme: 48 fallthrough 49 case dir.Scheme: 50 fallthrough 51 case git.Scheme: 52 absPath, err := filepath.Abs(strings.TrimPrefix(aURI.Opaque, "//")) 53 if err != nil { 54 return err 55 } 56 if aURI.Scheme == "" { 57 aURI.Opaque = absPath 58 } else { 59 aURI.Opaque = "//" + absPath 60 } 61 opts.arg = aURI.String() 62 m["path"] = absPath 63 } 64 65 hostname, _ := os.Hostname() 66 if hostname != "" { 67 m["hostname"] = hostname 68 } 69 70 if opts.name == "" { 71 opts.name = hostname 72 } 73 74 alertConfig, err := u.CreateAlert(opts.name, opts.email, a, v, m) 75 if err != nil { 76 return fmt.Errorf("cannot create alert: %s", err) 77 } 78 79 if output == "" { 80 fmt.Printf("\nAlert %s has been created.\n", alertConfig.AlertUUID) 81 } 82 83 if err := store.SaveAlert(u.Email(), alertConfig.AlertUUID, store.Alert{ 84 Name: opts.name, 85 Arg: opts.arg, 86 Config: alertConfig, 87 }); err != nil { 88 return fmt.Errorf("cannot save alert: %s", err) 89 } 90 91 if output == "" { 92 configPath, _ := store.AlertFilepath(u.Email()) 93 fmt.Printf("\nThe alert configuration has been added to %s.\n", configPath) 94 } 95 96 return nil 97 }