Skip to content

Instantly share code, notes, and snippets.

@niski84
Last active December 12, 2023 17:56
Show Gist options
  • Save niski84/1568adae441c5bf4ae4804e677ed3255 to your computer and use it in GitHub Desktop.
Save niski84/1568adae441c5bf4ae4804e677ed3255 to your computer and use it in GitHub Desktop.
collect input and pass to func
package main
import (
"fmt"
"os"
"strings"
"flag"
)
type Input struct {
JiraSessionKey string // jira_seshun_key
IssueNumber string // issue-numbr
}
func main() {
var input Input
// Parse the "--jira-session-key" argument
flag.StringVar(&input.JiraSessionKey, "jira-session-key", "", "Jira session key (jira-seshun-key)")
flag.Parse()
// Check for environment variable "jira_session_key" if not provided via argument
if input.JiraSessionKey == "" {
jiraSessionKey := os.Getenv("jira_session_key")
if jiraSessionKey == "" {
// Prompt user for input if neither the argument nor the environment variable is set
fmt.Print("Enter Jira session key (jira-seshun-key): ")
fmt.Scanln(&input.JiraSessionKey)
}
}
// Parse the "--issue-number" argument
flag.StringVar(&input.IssueNumber, "issue-number", "", "Issue number (issue-numbr)")
flag.Parse()
// Check if issue number is blank and handle the error
if input.IssueNumber == "" {
fmt.Println("Error: Issue number cannot be blank.")
return
}
// Call the function with the collected input
result := someLibraryFunction(input)
// Do something with the result
fmt.Println("Result:", result)
}
func someLibraryFunction(input Input) string {
// Perform some action with the input
// ...
return "Library function executed successfully."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment