Skip to content

Commit 33b9329

Browse files
Merge pull request #393 from 1Password/amanda/huggingface
Amanda/huggingface
2 parents 6f8040b + 234fb2b commit 33b9329

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package huggingface
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/needsauth"
6+
"github.com/1Password/shell-plugins/sdk/schema"
7+
"github.com/1Password/shell-plugins/sdk/schema/credname"
8+
)
9+
10+
func HuggingFaceCLI() schema.Executable {
11+
return schema.Executable{
12+
Name: "HuggingFace CLI",
13+
Runs: []string{"huggingface-cli"},
14+
DocsURL: sdk.URL("https://huggingface.co/docs/huggingface_hub/quick-start"),
15+
NeedsAuth: needsauth.IfAll(
16+
needsauth.NotForHelpOrVersion(),
17+
needsauth.NotWithoutArgs(),
18+
needsauth.NotWhenContainsArgs("login"),
19+
needsauth.NotWhenContainsArgs("logout"),
20+
),
21+
Uses: []schema.CredentialUsage{
22+
{
23+
Name: credname.APIToken,
24+
},
25+
},
26+
}
27+
}

plugins/huggingface/plugin.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package huggingface
2+
3+
import (
4+
"github.com/1Password/shell-plugins/sdk"
5+
"github.com/1Password/shell-plugins/sdk/schema"
6+
)
7+
8+
func New() schema.Plugin {
9+
return schema.Plugin{
10+
Name: "huggingface",
11+
Platform: schema.PlatformInfo{
12+
Name: "Hugging Face",
13+
Homepage: sdk.URL("https://huggingface.co"),
14+
},
15+
Credentials: []schema.CredentialType{
16+
UserAccessToken(),
17+
},
18+
Executables: []schema.Executable{
19+
HuggingFaceCLI(),
20+
},
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package huggingface
2+
3+
import (
4+
"context"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/importer"
8+
"github.com/1Password/shell-plugins/sdk/provision"
9+
"github.com/1Password/shell-plugins/sdk/schema"
10+
"github.com/1Password/shell-plugins/sdk/schema/credname"
11+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
12+
)
13+
14+
func UserAccessToken() schema.CredentialType {
15+
return schema.CredentialType{
16+
Name: credname.APIToken,
17+
DocsURL: sdk.URL("https://huggingface.co/docs/hub/security-tokens"),
18+
ManagementURL: sdk.URL("https://huggingface.co/settings/tokens"),
19+
Fields: []schema.CredentialField{
20+
{
21+
Name: fieldname.UserAccessToken,
22+
MarkdownDescription: "Token used to authenticate to HuggingFace.",
23+
Secret: true,
24+
Composition: &schema.ValueComposition{
25+
Length: 37,
26+
Prefix: "hf_",
27+
Charset: schema.Charset{
28+
Uppercase: true,
29+
Lowercase: true,
30+
Symbols: true,
31+
},
32+
},
33+
},
34+
{
35+
Name: fieldname.Endpoint,
36+
MarkdownDescription: "Endpoint used to connect to HuggingFace CLI",
37+
Optional: true,
38+
Secret: false,
39+
Composition: &schema.ValueComposition{
40+
Charset: schema.Charset{
41+
Uppercase: true,
42+
Lowercase: true,
43+
Digits: true,
44+
Symbols: true,
45+
},
46+
},
47+
},
48+
{
49+
Name: fieldname.APIUrl,
50+
MarkdownDescription: "HF Inference Endpoint used to connect to HuggingFace CLI",
51+
Optional: true,
52+
Secret: false,
53+
Composition: &schema.ValueComposition{
54+
Charset: schema.Charset{
55+
Uppercase: true,
56+
Lowercase: true,
57+
Digits: true,
58+
Symbols: true,
59+
},
60+
},
61+
},
62+
},
63+
DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping),
64+
Importer: importer.TryAll(
65+
importer.TryEnvVarPair(defaultEnvVarMapping),
66+
TryHuggingFaceTokenFile(),
67+
)}
68+
}
69+
70+
var defaultEnvVarMapping = map[string]sdk.FieldName{
71+
"HUGGING_FACE_HUB_TOKEN": fieldname.UserAccessToken,
72+
"HF_ENDPOINT": fieldname.Endpoint,
73+
"HF_INFERENCE_ENDPOINT": fieldname.APIUrl,
74+
}
75+
76+
func TryHuggingFaceTokenFile() sdk.Importer {
77+
return importer.TryFile("~/.cache/huggingface/token", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {
78+
fileData := string(contents)
79+
80+
out.AddCandidate(sdk.ImportCandidate{
81+
Fields: map[sdk.FieldName]string{
82+
fieldname.UserAccessToken: fileData,
83+
},
84+
})
85+
})
86+
87+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package huggingface
2+
3+
import (
4+
"testing"
5+
6+
"github.com/1Password/shell-plugins/sdk"
7+
"github.com/1Password/shell-plugins/sdk/plugintest"
8+
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
9+
)
10+
11+
func TestAPITokenProvisioner(t *testing.T) {
12+
plugintest.TestProvisioner(
13+
t, UserAccessToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{
14+
"default": {
15+
ItemFields: map[sdk.FieldName]string{
16+
fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE",
17+
fieldname.Endpoint: "https://huggingface.co",
18+
fieldname.APIUrl: "https://api-inference.huggingface.com",
19+
},
20+
ExpectedOutput: sdk.ProvisionOutput{
21+
Environment: map[string]string{
22+
"HUGGING_FACE_HUB_TOKEN": "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE",
23+
"HF_ENDPOINT": "https://huggingface.co",
24+
"HF_INFERENCE_ENDPOINT": "https://api-inference.huggingface.com",
25+
},
26+
},
27+
},
28+
})
29+
}
30+
31+
func TestAPITokenImporter(t *testing.T) {
32+
plugintest.TestImporter(
33+
t, UserAccessToken().Importer, map[string]plugintest.ImportCase{
34+
"config file (macOS)": {
35+
Files: map[string]string{
36+
"~/.cache/huggingface/token": plugintest.LoadFixture(t, "token"),
37+
},
38+
ExpectedCandidates: []sdk.ImportCandidate{
39+
{
40+
Fields: map[sdk.FieldName]string{
41+
fieldname.UserAccessToken: "hf_yVvZeburdKtnwkVCWPXimmNwaFuEXAMPLE\n",
42+
},
43+
},
44+
},
45+
},
46+
},
47+
)
48+
}

sdk/schema/fieldname/names.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import "github.com/1Password/shell-plugins/sdk"
55
// Credential field names.
66
const (
77
APIHost = sdk.FieldName("API Host")
8+
APIUrl = sdk.FieldName("API URL")
89
APIKey = sdk.FieldName("API Key")
910
APIKeyID = sdk.FieldName("API Key ID")
1011
APISecret = sdk.FieldName("API Secret")
@@ -53,6 +54,7 @@ const (
5354
Token = sdk.FieldName("Token")
5455
URL = sdk.FieldName("URL")
5556
User = sdk.FieldName("User")
57+
UserAccessToken = sdk.FieldName("User Access Token")
5658
Username = sdk.FieldName("Username")
5759
Website = sdk.FieldName("Website")
5860
)

0 commit comments

Comments
 (0)