-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
53 changed files
with
2,184 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"runtime/pprof" | ||
"runtime/trace" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/sevlyar/go-daemon" | ||
) | ||
|
||
// connectCommand is the entrypoint for the connect command. | ||
func connectCommand() error { | ||
var socketPath string | ||
|
||
label, err := getSocketPath(CLI.Socket) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed to detect socket path: %s", | ||
err, | ||
) | ||
} | ||
socketPath = label | ||
|
||
if daemon.WasReborn() { | ||
cntx := new(daemon.Context) | ||
_, err := cntx.Reborn() | ||
if err != nil { | ||
return fmt.Errorf("failed to reincarnate") | ||
} | ||
|
||
defer func() { | ||
if err := cntx.Release(); err != nil { | ||
log.Panic().Err(err).Msg("unable to release pid-file") | ||
} | ||
}() | ||
|
||
if len(CLI.Connect.CPU) > 0 { | ||
f, err := os.Create(CLI.Connect.CPU) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"unable to create %s: %s", | ||
CLI.Connect.CPU, | ||
err, | ||
) | ||
} | ||
defer f.Close() | ||
if err := pprof.StartCPUProfile(f); err != nil { | ||
return fmt.Errorf( | ||
"could not start CPU profile: %s", | ||
err, | ||
) | ||
} | ||
defer pprof.StopCPUProfile() | ||
} | ||
|
||
if len(CLI.Connect.Trace) > 0 { | ||
f, err := os.Create(CLI.Connect.Trace) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"unable to create %s: %s", | ||
CLI.Connect.Trace, | ||
err, | ||
) | ||
} | ||
defer f.Close() | ||
if err := trace.Start(f); err != nil { | ||
return fmt.Errorf( | ||
"could not start trace profile: %s", | ||
err, | ||
) | ||
} | ||
defer trace.Stop() | ||
} | ||
|
||
err = serve(socketPath) | ||
if err != nil && err != http.ErrServerClosed { | ||
return fmt.Errorf( | ||
"failed to start cy: %s", | ||
err, | ||
) | ||
} | ||
return nil | ||
} | ||
|
||
conn, err := connect(socketPath, true) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed to start cy: %s", | ||
err, | ||
) | ||
} | ||
|
||
err = poll(conn) | ||
if err != nil { | ||
return fmt.Errorf( | ||
"failed while polling: %s", | ||
err, | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/cfoust/cy/pkg/cy" | ||
) | ||
|
||
func getContext() (socket string, id int, ok bool) { | ||
context, ok := os.LookupEnv(cy.CONTEXT_ENV) | ||
if !ok { | ||
return "", 0, false | ||
} | ||
|
||
match := cy.CONTEXT_REGEX.FindStringSubmatch(context) | ||
if match == nil { | ||
return "", 0, false | ||
} | ||
|
||
socket = match[cy.CONTEXT_REGEX.SubexpIndex("socket")] | ||
id, _ = strconv.Atoi(match[cy.CONTEXT_REGEX.SubexpIndex("id")]) | ||
ok = true | ||
return | ||
} | ||
|
||
// execCommand is the entrypoint for the exec command. | ||
func execCommand() error { | ||
if CLI.Exec.Command == "" && CLI.Exec.File == "" { | ||
return fmt.Errorf("no Janet code provided") | ||
} | ||
|
||
var err error | ||
var source string | ||
var code []byte | ||
|
||
if CLI.Exec.Command != "" { | ||
source = "<unknown>" | ||
code = []byte(CLI.Exec.Command) | ||
} else if CLI.Exec.File == "-" { | ||
source = "<stdin>" | ||
code, err = ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
return fmt.Errorf("failed to read from stdin: %s", err) | ||
} | ||
} else { | ||
source = CLI.Exec.File | ||
code, err = ioutil.ReadFile(CLI.Exec.File) | ||
if err != nil { | ||
return fmt.Errorf("failed to read from %s: %s", CLI.Exec.File, err) | ||
} | ||
} | ||
|
||
socket, id, ok := getContext() | ||
if !ok { | ||
socket = CLI.Socket | ||
} | ||
|
||
socketPath, err := getSocketPath(socket) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var conn Connection | ||
conn, err = connect(socketPath, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
format := OutputFormatRaw | ||
switch CLI.Exec.Format { | ||
case "raw": | ||
format = OutputFormatRaw | ||
case "json": | ||
format = OutputFormatJSON | ||
case "janet": | ||
format = OutputFormatJanet | ||
default: | ||
return fmt.Errorf( | ||
"unknown output format: %s", | ||
CLI.Exec.Format, | ||
) | ||
} | ||
|
||
response, err := RPC[RPCExecArgs, RPCExecResponse]( | ||
conn, | ||
RPCExec, | ||
RPCExecArgs{ | ||
Source: source, | ||
Code: code, | ||
Node: id, | ||
Format: format, | ||
}, | ||
) | ||
if err != nil || len(response.Data) == 0 { | ||
return err | ||
} | ||
|
||
_, err = os.Stdout.Write(response.Data) | ||
return err | ||
} |
Oops, something went wrong.