[#154] Improve UX for rejected / allowed files

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-12-01 18:54:46 +01:00
parent dc47bf0861
commit 3ebc896169
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
3 changed files with 10 additions and 2 deletions

View file

@ -45,6 +45,8 @@ func init() {
} }
func createRunE(cmd *cobra.Command, _ []string) (err error) { func createRunE(cmd *cobra.Command, _ []string) (err error) {
cmd.SilenceUsage = true
var secret client.Secret var secret client.Secret
if client.HTTPClient, err = constructHTTPClient(cmd); err != nil { if client.HTTPClient, err = constructHTTPClient(cmd); err != nil {

View file

@ -16,7 +16,7 @@ import (
const storeFileMode = 0o600 // We assume the attached file to be a secret const storeFileMode = 0o600 // We assume the attached file to be a secret
var fetchCmd = &cobra.Command{ var fetchCmd = &cobra.Command{
Use: "fetch url", Use: "fetch <url>",
Short: "Retrieves a secret from the instance by its URL", Short: "Retrieves a secret from the instance by its URL",
Long: "", Long: "",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
@ -39,6 +39,8 @@ func checkDirWritable(dir string) error {
} }
func fetchRunE(cmd *cobra.Command, args []string) error { func fetchRunE(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
fileDir, err := cmd.Flags().GetString("file-dir") fileDir, err := cmd.Flags().GetString("file-dir")
if err != nil { if err != nil {
return fmt.Errorf("getting file-dir parameter: %w", err) return fmt.Errorf("getting file-dir parameter: %w", err)

View file

@ -73,12 +73,15 @@ func SanityCheck(instanceURL string, secret Secret) error {
func attachmentAllowed(file SecretAttachment, allowed []string) bool { func attachmentAllowed(file SecretAttachment, allowed []string) bool {
mimeType, _, _ := strings.Cut(file.Type, ";") mimeType, _, _ := strings.Cut(file.Type, ";")
logger := Logger.WithField("content-type", mimeType)
for _, a := range allowed { for _, a := range allowed {
switch { switch {
case mimeRegex.MatchString(a): case mimeRegex.MatchString(a):
// That's a mime type // That's a mime type
if glob.Glob(a, mimeType) { if glob.Glob(a, mimeType) {
// The mime "glob" matches the file type // The mime "glob" matches the file type
logger.WithField("allowed_by", a).Debug("attachment allowed")
return true return true
} }
@ -86,12 +89,13 @@ func attachmentAllowed(file SecretAttachment, allowed []string) bool {
// That's a file extension // That's a file extension
if strings.HasSuffix(file.Name, a) { if strings.HasSuffix(file.Name, a) {
// The filename has the right extension // The filename has the right extension
logger.WithField("allowed_by", a).Debug("attachment allowed")
return true return true
} }
} }
} }
Logger.WithField("content-type", mimeType).Debug("attachment type not allowed") logger.Debug("attachment type not allowed")
return false return false
} }