func AutoAddResource(fileName string, gossConfig GossConfig, key string, c *cli.Context, config util.Config, sys *system.System) error { // file if strings.Contains(key, "/") { if res, _, ok := gossConfig.Files.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } } // group if res, _, ok := gossConfig.Groups.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // package if res, _, ok := gossConfig.Packages.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // port if res, _, ok := gossConfig.Ports.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // process if res, sysres, ok := gossConfig.Processes.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) ports := system.GetPorts(true) pids, _ := sysres.Pids() for _, pid := range pids { pidS := strconv.Itoa(pid) for port, entries := range ports { for _, entry := range entries { if entry.Pid == pidS { // port if res, _, ok := gossConfig.Ports.AppendSysResourceIfExists(port, sys); ok == true { resourcePrint(fileName, res) } } } } } } // Service if res, _, ok := gossConfig.Services.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // user if res, _, ok := gossConfig.Users.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } return nil }
func AutoAppendResource(fileName, key string, c *cli.Context) error { var configJSON ConfigJSON if _, err := os.Stat(fileName); err == nil { configJSON = ReadJSON(fileName) } else { configJSON = *NewConfigJSON() } sys := system.New(c) // file if strings.Contains(key, "/") { if res, _, ok := configJSON.Files.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } } // group if res, _, ok := configJSON.Groups.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // package if res, _, ok := configJSON.Packages.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // port if res, _, ok := configJSON.Ports.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // process if res, sysres, ok := configJSON.Processes.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) ports := system.GetPorts(true) pids, _ := sysres.Pids() for _, pid := range pids { pidS := strconv.Itoa(pid) for port, entry := range ports { if entry.Pid == pidS { // port if res, _, ok := configJSON.Ports.AppendSysResourceIfExists(port, sys); ok == true { resourcePrint(fileName, res) } } } } } // Service if res, _, ok := configJSON.Services.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } // user if res, _, ok := configJSON.Users.AppendSysResourceIfExists(key, sys); ok == true { resourcePrint(fileName, res) } WriteJSON(fileName, configJSON) return nil }