Skip to content

Commit

Permalink
remove service command
Browse files Browse the repository at this point in the history
  • Loading branch information
pomdtr committed Dec 11, 2023
1 parent ca31d62 commit 64aa023
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 106 deletions.
27 changes: 0 additions & 27 deletions com.pomdtr.tweety.plist

This file was deleted.

128 changes: 62 additions & 66 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,16 @@ package main

import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"runtime"
"text/template"

_ "embed"

"github.com/phayes/freeport"
"github.com/spf13/cobra"
)

//go:embed com.pomdtr.tweety.plist
var launchdService []byte

func LaunchdService(write io.Writer) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}

execPath, err := os.Executable()
if err != nil {
return err
}

tmpl := template.New("service")
tmpl.Parse(string(launchdService))
return tmpl.Execute(os.Stdout, map[string]interface{}{
"ExecPath": execPath,
"HomeDir": homeDir,
})
}

//go:embed tweety.service
var systemdService []byte

func SystemdService(write io.Writer) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}

execPath, err := os.Executable()
if err != nil {
return err
}

tmpl := template.New("service")
tmpl.Parse(string(systemdService))
return tmpl.Execute(os.Stdout, map[string]interface{}{
"ExecPath": execPath,
"HomeDir": homeDir,
})
}

func NewCmdService() *cobra.Command {
cmd := &cobra.Command{
Use: "service",
RunE: func(cmd *cobra.Command, args []string) error {
switch runtime.GOOS {
case "darwin":
return LaunchdService(os.Stdout)
case "linux":
return SystemdService(os.Stdout)
default:
return fmt.Errorf("unsupported platform: %s", runtime.GOOS)
}
},
}

return cmd
}

func main() {
var flags struct {
host string
Expand Down Expand Up @@ -120,8 +55,69 @@ func main() {
cmd.Flags().StringVarP(&flags.host, "host", "H", "localhost", "host to listen on")
cmd.Flags().IntVarP(&flags.port, "port", "p", 9999, "port to listen on")

cmd.AddCommand(NewCmdService())
cmd.AddCommand(NewCompletionCmd(cmd.Name()))
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}

func NewCompletionCmd(name string) *cobra.Command {
return &cobra.Command{
Use: "completion [bash|zsh|fish|powershell]",
Short: "Generate completion script",
Long: fmt.Sprintf(`To load completions:
Bash:
$ source <(%[1]s completion bash)
# To load completions for each session, execute once:
# Linux:
$ %[1]s completion bash > /etc/bash_completion.d/%[1]s
# macOS:
$ %[1]s completion bash > $(brew --prefix)/etc/bash_completion.d/%[1]s
Zsh:
# If shell completion is not already enabled in your environment,
# you will need to enable it. You can execute the following once:
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
# To load completions for each session, execute once:
$ %[1]s completion zsh > "${fpath[1]}/_%[1]s"
# You will need to start a new shell for this setup to take effect.
fish:
$ %[1]s completion fish | source
# To load completions for each session, execute once:
$ %[1]s completion fish > ~/.config/fish/completions/%[1]s.fish
PowerShell:
PS> %[1]s completion powershell | Out-String | Invoke-Expression
# To load completions for every new session, run:
PS> %[1]s completion powershell > %[1]s.ps1
# and source this file from your PowerShell profile.
`, name),
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cmd.Root().GenBashCompletion(os.Stdout)
case "zsh":
cmd.Root().GenZshCompletion(os.Stdout)
case "fish":
cmd.Root().GenFishCompletion(os.Stdout, true)
case "powershell":
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
}
},
}
}
13 changes: 0 additions & 13 deletions tweety.service

This file was deleted.

0 comments on commit 64aa023

Please sign in to comment.