mirror of
https://github.com/F1bonacc1/process-compose.git
synced 2024-09-11 11:35:26 +03:00
Add remote client support
Added methods: list, start, stop
This commit is contained in:
parent
5c49172cbc
commit
c8bb6879dd
@ -49,6 +49,10 @@ type ProcessState struct {
|
||||
Pid int `json:"pid"`
|
||||
}
|
||||
|
||||
type ProcessStates struct {
|
||||
States []ProcessState `json:"data"`
|
||||
}
|
||||
|
||||
func (p ProcessConfig) GetDependencies() []string {
|
||||
dependencies := make([]string, len(p.DependsOn))
|
||||
|
||||
|
5
src/client/common.go
Normal file
5
src/client/common.go
Normal file
@ -0,0 +1,5 @@
|
||||
package client
|
||||
|
||||
type pcError struct {
|
||||
Error string `json:"error"`
|
||||
}
|
29
src/client/processes.go
Normal file
29
src/client/processes.go
Normal file
@ -0,0 +1,29 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/f1bonacc1/process-compose/src/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func GetProcessesName(address string, port int) ([]string, error) {
|
||||
url := fmt.Sprintf("http://%s:%d/processes", address, port)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
//Create a variable of the same type as our model
|
||||
var sResp app.ProcessStates
|
||||
|
||||
//Decode the data
|
||||
if err := json.NewDecoder(resp.Body).Decode(&sResp); err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
procs := make([]string, len(sResp.States))
|
||||
for i, proc := range sResp.States {
|
||||
procs[i] = proc.Name
|
||||
}
|
||||
return procs, nil
|
||||
}
|
26
src/client/start.go
Normal file
26
src/client/start.go
Normal file
@ -0,0 +1,26 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func StartProcesses(address string, port int, name string) error {
|
||||
url := fmt.Sprintf("http://%s:%d/process/start/%s", address, port, name)
|
||||
resp, err := http.Post(url, "application/json", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var respErr pcError
|
||||
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
|
||||
log.Error().Msgf("failed to decode start process %s response: %v", name, err)
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf(respErr.Error)
|
||||
}
|
31
src/client/stop.go
Normal file
31
src/client/stop.go
Normal file
@ -0,0 +1,31 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func StopProcesses(address string, port int, name string) error {
|
||||
url := fmt.Sprintf("http://%s:%d/process/stop/%s", address, port, name)
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest(http.MethodPatch, url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var respErr pcError
|
||||
if err = json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
|
||||
log.Error().Msgf("failed to decode stop process %s response: %v", name, err)
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf(respErr.Error)
|
||||
}
|
30
src/cmd/list.go
Normal file
30
src/cmd/list.go
Normal file
@ -0,0 +1,30 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/f1bonacc1/process-compose/src/client"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// listCmd represents the list command
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List running processes",
|
||||
Aliases: []string{"ls"},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
processNames, err := client.GetProcessesName(pcAddress, port)
|
||||
if err != nil {
|
||||
log.Error().Msgf("Failed to get processes names %v", err)
|
||||
return
|
||||
}
|
||||
for _, proc := range processNames {
|
||||
fmt.Println(proc)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
processCmd.AddCommand(listCmd)
|
||||
}
|
21
src/cmd/process.go
Normal file
21
src/cmd/process.go
Normal file
@ -0,0 +1,21 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
pcAddress string
|
||||
)
|
||||
|
||||
// processCmd represents the process command
|
||||
var processCmd = &cobra.Command{
|
||||
Use: "process",
|
||||
Short: "Execute operations on running processes",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(processCmd)
|
||||
processCmd.PersistentFlags().StringVarP(&pcAddress, "address", "a", "localhost", "address of a running process compose server")
|
||||
}
|
@ -28,13 +28,7 @@ var (
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
rootCmd = &cobra.Command{
|
||||
Use: "process-compose",
|
||||
Short: "A brief description of your application",
|
||||
Long: `A longer description that spans multiple lines and likely contains
|
||||
examples and usage of using your application. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
Short: "Processes scheduler and orchestrator",
|
||||
// Uncomment the following line if your bare application
|
||||
// has an action associated with it:
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
@ -103,7 +97,7 @@ func init() {
|
||||
|
||||
rootCmd.Flags().StringVarP(&fileName, "config", "f", app.DefaultFileNames[0], "path to config file to load")
|
||||
rootCmd.Flags().BoolVarP(&isTui, "tui", "t", true, "disable tui (-t=false)")
|
||||
rootCmd.Flags().IntVarP(&port, "port", "p", 8080, "port number")
|
||||
rootCmd.PersistentFlags().IntVarP(&port, "port", "p", 8080, "port number")
|
||||
}
|
||||
|
||||
func runHeadless(project *app.Project) {
|
||||
|
31
src/cmd/start.go
Normal file
31
src/cmd/start.go
Normal file
@ -0,0 +1,31 @@
|
||||
/*
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/f1bonacc1/process-compose/src/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// startCmd represents the start command
|
||||
var startCmd = &cobra.Command{
|
||||
Use: "start [PROCESS]",
|
||||
Short: "Start process",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
name := args[0]
|
||||
err := client.StartProcesses(pcAddress, port, name)
|
||||
if err != nil {
|
||||
log.Error().Msgf("Failed to start processes %s: %v", name, err)
|
||||
return
|
||||
}
|
||||
log.Info().Msgf("Process %s started", name)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
processCmd.AddCommand(startCmd)
|
||||
}
|
30
src/cmd/stop.go
Normal file
30
src/cmd/stop.go
Normal file
@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/f1bonacc1/process-compose/src/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// stopCmd represents the stop command
|
||||
var stopCmd = &cobra.Command{
|
||||
Use: "stop [PROCESS]",
|
||||
Short: "Stop a running process",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
name := args[0]
|
||||
err := client.StopProcesses(pcAddress, port, name)
|
||||
if err != nil {
|
||||
log.Error().Msgf("Failed to stop processes %s: %v", name, err)
|
||||
return
|
||||
}
|
||||
log.Info().Msgf("Process %s stopped", name)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
processCmd.AddCommand(stopCmd)
|
||||
}
|
Loading…
Reference in New Issue
Block a user