macOS
This commit is contained in:
		
							parent
							
								
									f63b15ba5a
								
							
						
					
					
						commit
						6d601ad141
					
				
					 7 changed files with 102 additions and 90 deletions
				
			
		|  | @ -63,7 +63,7 @@ Examples: | |||
| Please also check out the docs on publishing messages. Especially for the --tags and --delay options,  | ||||
| it has incredibly useful information: https://ntfy.sh/docs/publish/. | ||||
| 
 | ||||
| ` + defaultClientConfigFileDescriptionSuffix, | ||||
| ` + clientCommandDescriptionSuffix, | ||||
| } | ||||
| 
 | ||||
| func execPublish(c *cli.Context) error { | ||||
|  |  | |||
|  | @ -6,7 +6,11 @@ import ( | |||
| 	"github.com/urfave/cli/v2" | ||||
| 	"heckel.io/ntfy/client" | ||||
| 	"heckel.io/ntfy/util" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"os/user" | ||||
| 	"path/filepath" | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
|  | @ -14,6 +18,12 @@ func init() { | |||
| 	commands = append(commands, cmdSubscribe) | ||||
| } | ||||
| 
 | ||||
| const ( | ||||
| 	clientRootConfigFileUnixAbsolute    = "/etc/ntfy/client.yml" | ||||
| 	clientUserConfigFileUnixRelative    = "ntfy/client.yml" | ||||
| 	clientUserConfigFileWindowsRelative = "ntfy\\client.yml" | ||||
| ) | ||||
| 
 | ||||
| var cmdSubscribe = &cli.Command{ | ||||
| 	Name:      "subscribe", | ||||
| 	Aliases:   []string{"sub"}, | ||||
|  | @ -71,7 +81,7 @@ ntfy subscribe --from-config | |||
|     ntfy sub --from-config                           # Read topics from config file | ||||
|     ntfy sub --config=myclient.yml --from-config     # Read topics from alternate config file | ||||
| 
 | ||||
| ` + defaultClientConfigFileDescriptionSuffix, | ||||
| ` + clientCommandDescriptionSuffix, | ||||
| } | ||||
| 
 | ||||
| func execSubscribe(c *cli.Context) error { | ||||
|  | @ -195,6 +205,24 @@ func runCommand(c *cli.Context, command string, m *client.Message) { | |||
| 	} | ||||
| } | ||||
| 
 | ||||
| func runCommandInternal(c *cli.Context, script string, m *client.Message) error { | ||||
| 	scriptFile := fmt.Sprintf("%s/ntfy-subscribe-%s.%s", os.TempDir(), util.RandomString(10), scriptExt) | ||||
| 	if err := os.WriteFile(scriptFile, []byte(scriptHeader+script), 0700); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer os.Remove(scriptFile) | ||||
| 	verbose := c.Bool("verbose") | ||||
| 	if verbose { | ||||
| 		log.Printf("[%s] Executing: %s (for message: %s)", util.ShortTopicURL(m.TopicURL), script, m.Raw) | ||||
| 	} | ||||
| 	cmd := exec.Command(scriptLauncher[0], append(scriptLauncher[1:], scriptFile)...) | ||||
| 	cmd.Stdin = c.App.Reader | ||||
| 	cmd.Stdout = c.App.Writer | ||||
| 	cmd.Stderr = c.App.ErrWriter | ||||
| 	cmd.Env = envVars(m) | ||||
| 	return cmd.Run() | ||||
| } | ||||
| 
 | ||||
| func envVars(m *client.Message) []string { | ||||
| 	env := os.Environ() | ||||
| 	env = append(env, envVar(m.ID, "NTFY_ID", "id")...) | ||||
|  | @ -227,3 +255,18 @@ func loadConfig(c *cli.Context) (*client.Config, error) { | |||
| 	} | ||||
| 	return client.NewConfig(), nil | ||||
| } | ||||
| 
 | ||||
| func defaultConfigFileUnix() string { | ||||
| 	u, _ := user.Current() | ||||
| 	configFile := clientRootConfigFileUnixAbsolute | ||||
| 	if u.Uid != "0" { | ||||
| 		homeDir, _ := os.UserConfigDir() | ||||
| 		return filepath.Join(homeDir, clientUserConfigFileUnixRelative) | ||||
| 	} | ||||
| 	return configFile | ||||
| } | ||||
| 
 | ||||
| func defaultConfigFileWindows() string { | ||||
| 	homeDir, _ := os.UserConfigDir() | ||||
| 	return filepath.Join(homeDir, clientUserConfigFileWindowsRelative) | ||||
| } | ||||
|  |  | |||
							
								
								
									
										16
									
								
								cmd/subscribe_darwin.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								cmd/subscribe_darwin.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | |||
| package cmd | ||||
| 
 | ||||
| const ( | ||||
| 	scriptExt                      = "sh" | ||||
| 	scriptHeader                   = "#!/bin/sh\n" | ||||
| 	clientCommandDescriptionSuffix = `The default config file for all client commands is /etc/ntfy/client.yml (if root user), | ||||
| or "~/Library/Application Support" for all other users.` | ||||
| ) | ||||
| 
 | ||||
| var ( | ||||
| 	scriptLauncher = []string{"sh", "-c"} | ||||
| ) | ||||
| 
 | ||||
| func defaultConfigFile() string { | ||||
| 	return defaultConfigFileUnix() | ||||
| } | ||||
|  | @ -1,57 +1,16 @@ | |||
| package cmd | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/urfave/cli/v2" | ||||
| 	"heckel.io/ntfy/client" | ||||
| 	"heckel.io/ntfy/util" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"os/user" | ||||
| 	"path/filepath" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	defaultClientRootConfigFile              = "/etc/ntfy/client.yml" | ||||
| 	defaultClientUserConfigFileRelative      = "ntfy/client.yml" | ||||
| 	defaultClientConfigFileDescriptionSuffix = `The default config file for all client commands is /etc/ntfy/client.yml (if root user), | ||||
| 	scriptExt                      = "sh" | ||||
| 	scriptHeader                   = "#!/bin/sh\n" | ||||
| 	clientCommandDescriptionSuffix = `The default config file for all client commands is /etc/ntfy/client.yml (if root user), | ||||
| or ~/.config/ntfy/client.yml for all other users.` | ||||
| ) | ||||
| 
 | ||||
| func runCommandInternal(c *cli.Context, command string, m *client.Message) error { | ||||
| 	scriptFile, err := createTmpScript(command) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer os.Remove(scriptFile) | ||||
| 	verbose := c.Bool("verbose") | ||||
| 	if verbose { | ||||
| 		log.Printf("[%s] Executing: %s (for message: %s)", util.ShortTopicURL(m.TopicURL), command, m.Raw) | ||||
| 	} | ||||
| 	cmd := exec.Command("sh", "-c", scriptFile) | ||||
| 	cmd.Stdin = c.App.Reader | ||||
| 	cmd.Stdout = c.App.Writer | ||||
| 	cmd.Stderr = c.App.ErrWriter | ||||
| 	cmd.Env = envVars(m) | ||||
| 	return cmd.Run() | ||||
| } | ||||
| 
 | ||||
| func createTmpScript(command string) (string, error) { | ||||
| 	scriptFile := fmt.Sprintf("%s/ntfy-subscribe-%s.sh.tmp", os.TempDir(), util.RandomString(10)) | ||||
| 	script := fmt.Sprintf("#!/bin/sh\n%s", command) | ||||
| 	if err := os.WriteFile(scriptFile, []byte(script), 0700); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return scriptFile, nil | ||||
| } | ||||
| var ( | ||||
| 	scriptLauncher = []string{"sh", "-c"} | ||||
| ) | ||||
| 
 | ||||
| func defaultConfigFile() string { | ||||
| 	u, _ := user.Current() | ||||
| 	configFile := defaultClientRootConfigFile | ||||
| 	if u.Uid != "0" { | ||||
| 		homeDir, _ := os.UserConfigDir() | ||||
| 		return filepath.Join(homeDir, defaultClientUserConfigFileRelative) | ||||
| 	} | ||||
| 	return configFile | ||||
| 	return defaultConfigFileUnix() | ||||
| } | ||||
|  |  | |||
|  | @ -1,48 +1,15 @@ | |||
| package cmd | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"github.com/urfave/cli/v2" | ||||
| 	"heckel.io/ntfy/client" | ||||
| 	"heckel.io/ntfy/util" | ||||
| 	"log" | ||||
| 	"os" | ||||
| 	"os/exec" | ||||
| 	"path/filepath" | ||||
| ) | ||||
| 
 | ||||
| const ( | ||||
| 	defaultClientUserConfigFileRelative      = "ntfy\\client.yml" | ||||
| 	defaultClientConfigFileDescriptionSuffix = `The default config file for all client commands is %AppData%\ntfy\client.yml.` | ||||
| 	scriptExt                      = "bat" | ||||
| 	scriptHeader                   = "" | ||||
| 	clientCommandDescriptionSuffix = `The default config file for all client commands is %AppData%\ntfy\client.yml.` | ||||
| ) | ||||
| 
 | ||||
| func runCommandInternal(c *cli.Context, command string, m *client.Message) error { | ||||
| 	scriptFile, err := createTmpScript(command) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
| 	defer os.Remove(scriptFile) | ||||
| 	verbose := c.Bool("verbose") | ||||
| 	if verbose { | ||||
| 		log.Printf("[%s] Executing: %s (for message: %s)", util.ShortTopicURL(m.TopicURL), command, m.Raw) | ||||
| 	} | ||||
| 	cmd := exec.Command("cmd.exe", "/Q", "/C", scriptFile) | ||||
| 	cmd.Stdin = c.App.Reader | ||||
| 	cmd.Stdout = c.App.Writer | ||||
| 	cmd.Stderr = c.App.ErrWriter | ||||
| 	cmd.Env = envVars(m) | ||||
| 	return cmd.Run() | ||||
| } | ||||
| 
 | ||||
| func createTmpScript(command string) (string, error) { | ||||
| 	scriptFile := fmt.Sprintf("%s/ntfy-subscribe-%s.bat", os.TempDir(), util.RandomString(10)) | ||||
| 	if err := os.WriteFile(scriptFile, []byte(command), 0700); err != nil { | ||||
| 		return "", err | ||||
| 	} | ||||
| 	return scriptFile, nil | ||||
| } | ||||
| var ( | ||||
| 	scriptLauncher = []string{"cmd.exe", "/Q", "/C"} | ||||
| ) | ||||
| 
 | ||||
| func defaultConfigFile() string { | ||||
| 	homeDir, _ := os.UserConfigDir() | ||||
| 	return filepath.Join(homeDir, defaultClientUserConfigFileRelative) | ||||
| 	return defaultConfigFileWindows() | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue