{"id":22930706,"url":"https://github.com/kubewarden/gostubpkg","last_synced_at":"2025-10-08T13:16:42.408Z","repository":{"id":198648464,"uuid":"700901744","full_name":"kubewarden/gostubpkg","owner":"kubewarden","description":"gostubpkg is a tool for generating stubs of Go packages","archived":false,"fork":false,"pushed_at":"2025-09-10T14:57:56.000Z","size":415,"stargazers_count":3,"open_issues_count":4,"forks_count":0,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-09-10T18:29:21.160Z","etag":null,"topics":["ast","binary-size","codegen","go","golang","stub","wasm"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kubewarden.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-10-05T14:13:23.000Z","updated_at":"2025-09-10T14:36:18.000Z","dependencies_parsed_at":"2024-01-10T15:28:56.932Z","dependency_job_id":"96ca3d03-840a-40ce-addc-5984aa9cdd6b","html_url":"https://github.com/kubewarden/gostubpkg","commit_stats":null,"previous_names":["fabriziosestito/go-stub-package","fabriziosestito/gostubpkg","kubewarden/gostubpkg"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/kubewarden/gostubpkg","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fgostubpkg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fgostubpkg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fgostubpkg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fgostubpkg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kubewarden","download_url":"https://codeload.github.com/kubewarden/gostubpkg/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kubewarden%2Fgostubpkg/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278949236,"owners_count":26073989,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ast","binary-size","codegen","go","golang","stub","wasm"],"created_at":"2024-12-14T10:29:25.239Z","updated_at":"2025-10-08T13:16:42.354Z","avatar_url":"https://github.com/kubewarden.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Stable](https://img.shields.io/badge/status-stable-brightgreen?style=for-the-badge)](https://github.com/kubewarden/community/blob/main/REPOSITORIES.md#stable)\n\n# gostubpkg\n\ngostubpkg is a tool for generating stubs of Go packages.\n\n## Goals and non-goals\n\nIn the context of gostubpkg, _stubbing_ means replacing the package with a dummy implementation that provides the same API as the original package.\nThis tool is intended to be used to slim down the size of Go binaries by replacing dependencies with stubs,\nspecifically when dealing with [WebAssembly](https://webassembly.org/) binaries.\nTherefore, this tool is not intended to be used for mocking or testing, as there are [other tools](https://github.com/avelino/awesome-go#testing) that are better suited for these purposes.\n\n## Installation\n\nYou can install gostubpkg using the following command:\n\n```shell\ngo get -u github.com/kubewarden/gostubpkg\n```\n\n## Usage\n\n```shell\ngostubpkg [flags] \u003cpatterns\u003e...\n\nFlags:\n  -a, --allow-imports strings            Specify this flag multiple times to add external imports\n                                         that will not be removed from the generated stubs.\n                                         Example: -a k8s.io/api/core/v1\n  -c, --config string                    config file (default \"gostubpkg.yaml\")\n  -f, --function-bodies stringToString   Specify this flag multiple times to add a type mapping.\n                                         Example: -f \"cmd.Execute\"='println(\"hello world\")' -f \"yourpkg.(*YourType).YourMethod\"='return nil' (default [])\n  -m, --generate-go-mod                  Generate the go.mod file in the root of the stub package\n  -h, --help                             help for gostubpkg\n  -i, --input-dir string                 Specify the directory in which to run the build system's query tool that provides information about the packages (default $PWD)\n  -o, --output-dir string                Specify the output directory for the stubs (default $PWD)\n  -v, --verbose count                    Increase output verbosity. Example: --verbose=2 or -vv\n```\n\n### Generate stubs for all packages\n\n```shell\ngostubpkg -m -i /path/to/module -o /path/to/output ./...\n```\n\nThis will generate stubs and a `go.mod` file for all packages in the specified input directory.\nAll the functions in the stubs will panic when called, and all the external imports will be removed.\nExternal types will be replaced with `interface{}` in struct fields, type aliases, and function signatures.\n\nPrivate functions, private struct fields, private struct methods, generated files, and test files will be ignored.\nPrivate types and interfaces will be kept in the stubs since they could be embedded in public types.\n\nFor instance, this code:\n\n```go\npackage yourpkg\n\nimport (\n    \"fmt\"\n    \"io\"\n\n    corev1 \"k8s.io/api/core/v1\"\n)\n\ntype YourType struct {\n    Pod corev1.Pod\n}\n\ntype yourOtherType struct {\n    Writer io.Writer\n    Foo()\n}\n\ntype YourAlias corev1.Pod\n\nfunc Foo(pod *corev1.Pod) {\n    fmt.Println(pod.Name)\n}\n\nfunc bar(pod *corev1.Pod) string  {\n    fmt.Println(pod.Name)\n}\n```\n\nWill be replaced with:\n\n```go\npackage yourpkg\n\nimport (\n    \"fmt\"\n    \"io\"\n)\n\ntype YourType struct {\n    Pod interface{}\n}\n\ntype yourOtherType struct {\n    Writer io.Writer\n    Foo()\n}\n\ntype YourAlias interface{}\n\nfunc Foo(pod interface{}) {\n    panic(\"stub\")\n}\n```\n\n### Generate stubs for specific packages and allow external imports\n\n```shell\ngostubpkg -i /path/to/your/code -o /path/to/output ./yourpkg --allow-imports k8s.io/api/core/v1\n```\n\nThis command generates stubs for packages in the specified input directory while allowing external imports from `k8s.io/api/core/v1`.\n\nFor instance, this code snippet:\n\n```go\npackage yourpkg\n\nimport (\n    corev1 \"k8s.io/api/core/v1\"\n)\n\ntype YourType struct {\n    Pod *corev1.Pod\n}\n\nfunc (t *YourType) SetPod(pod *corev1.Pod) {\n    t.Pod = pod\n}\n```\n\nWill be replaced with:\n\n```go\npackage yourpkg\n\nimport (\n    corev1 \"k8s.io/api/core/v1\"\n)\n\ntype YourType struct {\n    Pod corev1.Pod\n}\n\nfunc (t *YourType) SetPod(pod *corev1.Pod) {\n    panic(\"stub\")\n}\n```\n\n### Custom function bodies\n\nSometimes you may want to specify custom function bodies for the stubs.\n\n```shell\ngostubpkg -f \"yourpkg.Foo\"='println(\"hello stub\")' -f \"yourpkg.(*YourType).YourMethod\"='return nil'\n```\n\nThis code snippet:\n\n```go\npackage yourpkg\n\nfunc Foo() {\n    fmt.Println(\"hello world\")\n}\n\ntype YourType struct {}\n\nfunc (t *YourType) YourMethod() error {\n    return fmt.Errorf(\"error\")\n}\n```\n\nWill be replaced with:\n\n```go\npackage yourpkg\n\nfunc Foo() {\n    println(\"hello stub\")\n}\n\ntype YourType struct {}\n\nfunc (t *YourType) YourMethod() error {\n    return nil\n}\n```\n\n## Configuration\n\ngostubpkg supports a configuration file in YAML format.\nThe configuration file can be specified using the `--config` flag.\n\nExample `gostubpkg.yaml`:\n\n```yaml\nallow-imports:\n  - k8s.io/api/core/v1\n\ngenerate-go-mod: true\n\nfunction-bodies:\n  cmd.Execute: 'println(\"hello world\")'\n  yourpkg.(*YourType).YourMethod: \"return nil\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubewarden%2Fgostubpkg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkubewarden%2Fgostubpkg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkubewarden%2Fgostubpkg/lists"}