Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Don't Merge] サブコマンドのネスト対応 #904

Closed

Conversation

yamamoto-febc
Copy link
Member

@yamamoto-febc yamamoto-febc commented Apr 13, 2022

#899 の派生

サブコマンドの実装方法検討のための仮実装。

Platform - Resource - Commandという体系の中で、Resourceで子となるResourceを持てるようにする。
これによりusacloud phy server port listのようなサブコマンドが従来と同じようなコマンド定義で利用可能となる。

課題

  • init()での初期化が各所に散らばっているため集約したい

pkg/resources.goでリソース/コマンド定義するのをやめ、iaasやphyといったディレクトリの下で定義していく

  • 子リソースでのシェル補完をどう扱うか?
  • 子リソースでのListAllをどう扱うか?

core.Labelの出力処理を子リソースごとに実装 & パラメータ側でFindに渡すパラメータを生成するようにする

@yamamoto-febc
Copy link
Member Author

実装メモ: デフォルトのListAllの実装を以下のように修正

  • mapconvでコマンドパラメータから各サービスのFindRequestへ同名フィールドの値を引き渡し
  • もしコマンドパラメータが*service.FindRequest{}を返すfuncを実装していたらそちらを利用
func(ctx cli.Context, parameter interface{}) ([]interface{}, error) {
	svc := service.New(ctx.Client().(*phy.Client))
	req := &service.FindRequest{}

	// 修正1: mapconvで同名のフィールドを埋める
	if err := conv.ConvertTo(parameter, req); err != nil {
		return nil, err
	}

	// 修正2: *service.FindRequestを返すfuncを実装していたらそちらを利用
	type requester interface {
		FindRequest() *service.FindRequest
	}
	if v, ok := parameter.(requester); ok {
		req = v.FindRequest()
	}

	res, err := svc.FindWithContext(ctx, req)
	if err != nil {
		return nil, err
	}
	// ...
}

@yamamoto-febc
Copy link
Member Author

一部の実装を #905 でマージ済み。以降は #899 をリベースして作業を行う。

@yamamoto-febc yamamoto-febc deleted the feature/nested-resource branch August 22, 2022 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant