github.com/robgonnella/ardi/v2@v2.4.5-0.20230102052001-11a49de978c3/core/platform.go (about) 1 package core 2 3 import ( 4 "errors" 5 "fmt" 6 "sort" 7 "text/tabwriter" 8 9 log "github.com/sirupsen/logrus" 10 11 cli "github.com/robgonnella/ardi/v2/cli-wrapper" 12 ) 13 14 // PlatformCore module for platform commands 15 type PlatformCore struct { 16 logger *log.Logger 17 cli *cli.Wrapper 18 initialized bool 19 } 20 21 // PlatformCoreOption represents options for PlatformCore 22 type PlatformCoreOption = func(c *PlatformCore) 23 24 // NewPlatformCore platform module instance 25 func NewPlatformCore(logger *log.Logger, options ...PlatformCoreOption) *PlatformCore { 26 c := &PlatformCore{ 27 logger: logger, 28 initialized: false, 29 } 30 31 for _, o := range options { 32 o(c) 33 } 34 35 return c 36 } 37 38 // WithPlatformCliWrapper allows and injectable cli wrapper 39 func WithPlatformCliWrapper(wrapper *cli.Wrapper) PlatformCoreOption { 40 return func(c *PlatformCore) { 41 c.cli = wrapper 42 } 43 } 44 45 // ListInstalled lists only installed platforms 46 func (c *PlatformCore) ListInstalled() error { 47 platforms, err := c.cli.GetInstalledPlatforms() 48 if err != nil { 49 return err 50 } 51 52 sort.Slice(platforms, func(i, j int) bool { 53 return platforms[i].GetName() < platforms[j].GetName() 54 }) 55 56 w := tabwriter.NewWriter(c.logger.Out, 0, 0, 8, ' ', 0) 57 defer w.Flush() 58 w.Write([]byte("Platform\tID\tInstalled\n")) 59 for _, plat := range platforms { 60 w.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", plat.GetName(), plat.GetId(), plat.GetInstalled()))) 61 } 62 return nil 63 } 64 65 // ListAll lists all available platforms 66 func (c *PlatformCore) ListAll() error { 67 c.init() 68 69 platforms, err := c.cli.SearchPlatforms() 70 if err != nil { 71 return err 72 } 73 74 sort.Slice(platforms, func(i, j int) bool { 75 return platforms[i].GetName() < platforms[j].GetName() 76 }) 77 78 c.logger.Info("------AVAILABLE PLATFORMS------") 79 w := tabwriter.NewWriter(c.logger.Out, 0, 0, 8, ' ', 0) 80 defer w.Flush() 81 w.Write([]byte("Platform\tID\tLatest\n")) 82 for _, plat := range platforms { 83 w.Write([]byte(fmt.Sprintf("%s\t%s\t%s\n", plat.GetName(), plat.GetId(), plat.GetLatest()))) 84 } 85 return nil 86 } 87 88 // Add installs specified platforms 89 func (c *PlatformCore) Add(platform string) (string, string, error) { 90 c.init() 91 92 if platform == "" { 93 return "", "", errors.New("empty platform list") 94 } 95 96 installed, vers, err := c.cli.InstallPlatform(platform) 97 if err != nil { 98 return "", "", err 99 } 100 101 c.logger.Infof("Installed Platform: %s %s", installed, vers) 102 return installed, vers, nil 103 } 104 105 // Remove uninstalls specified platforms 106 func (c *PlatformCore) Remove(platform string) (string, error) { 107 if platform == "" { 108 return "", errors.New("empty platform list") 109 } 110 111 removed, err := c.cli.UninstallPlatform(platform) 112 if err != nil { 113 return "", err 114 } 115 116 return removed, nil 117 } 118 119 // private 120 func (c *PlatformCore) init() error { 121 if !c.initialized { 122 if err := c.cli.UpdatePlatformIndex(); err != nil { 123 return err 124 } 125 c.initialized = true 126 } 127 return nil 128 }