Skip to content

Commit

Permalink
refactor: more unified style
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 1, 2024
1 parent 6a7db29 commit d24989a
Show file tree
Hide file tree
Showing 43 changed files with 374 additions and 368 deletions.
8 changes: 4 additions & 4 deletions cmd/pkg/cli/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ func runApplyCommand(config ApplyConfig) func(cmd *cobra.Command, args []string)
}
}

exists, err := config.SpecStore.Load(ctx, specs...)
ok, err := config.SpecStore.Load(ctx, specs...)
if err != nil {
return err
}

var inserts []spec.Spec
var updates []spec.Spec
for _, sp := range specs {
if match := resourcebase.Match(sp, exists...); len(match) > 0 {
if match := resourcebase.Match(sp, ok...); len(match) > 0 {
sp.SetID(match[0].GetID())
updates = append(updates, sp)
} else {
Expand Down Expand Up @@ -103,15 +103,15 @@ func runApplyCommand(config ApplyConfig) func(cmd *cobra.Command, args []string)
}
}

exists, err := config.SecretStore.Load(ctx, secrets...)
ok, err := config.SecretStore.Load(ctx, secrets...)
if err != nil {
return err
}

var inserts []*secret.Secret
var updates []*secret.Secret
for _, sec := range secrets {
if match := resourcebase.Match(sec, exists...); len(match) > 0 {
if match := resourcebase.Match(sec, ok...); len(match) > 0 {
sec.SetID(match[0].GetID())
updates = append(updates, sec)
} else {
Expand Down
6 changes: 3 additions & 3 deletions cmd/pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var proc *process.Process
if len(args) > 1 {
id, _ := uuid.FromString(args[1])
proc, _ = m.agent.Process(id)
proc = m.agent.Process(id)
} else {
proc = m.debugger.Process()
}
Expand All @@ -277,14 +277,14 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var proc *process.Process
if len(args) > 1 {
id, _ := uuid.FromString(args[1])
proc, _ = m.agent.Process(id)
proc = m.agent.Process(id)
} else {
proc = m.debugger.Process()
}

var frames []*agent.Frame
if proc != nil {
frames, _ = m.agent.Frames(proc.ID())
frames = m.agent.Frames(proc.ID())
}

if frames == nil {
Expand Down
41 changes: 5 additions & 36 deletions docs/user_extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,10 @@ This code creates a new runtime environment using the provided schema, hook, spe

To integrate the runtime environment with existing services and build an executable, you need to set up the environment to run continuously or in a simpler execution mode.

### Continuous Execution

Running the runtime environment continuously allows it to handle external requests promptly. This approach is suitable for scenarios where ongoing workflow execution is needed.

```go
func main() {
ctx := context.TODO()

specStore := spec.NewStore()
secretStore := secret.NewStore()

Expand Down Expand Up @@ -372,39 +370,10 @@ func main() {
_ = r.Close()
}()

r.Listen(context.TODO())
r.Watch(ctx)
r.Load(ctx)
r.Reconcile(ctx)
}
```

This code keeps the runtime environment running and responsive to external signals. It uses `os.Signal` to listen for termination signals and safely shuts down the environment.

### Simple Execution

In some cases, you may prefer a simpler execution mode where the runtime environment runs only when needed and then exits. This approach is useful for scenarios where you don't need continuous operation.

```go
r := runtime.New(runtime.Config{
Namespace: "default",
Schema: scheme,
Hook: hook,
SpecStore: specStore,
SecretStore: secretStore,
})
defer r.Close()

r.Load(ctx) // Load all resources

symbols, _ := r.Load(ctx, &spec.Meta{
Name: "main",
})

sb := symbols[0]

in := port.NewOut()
defer in.Close()

in.Link(sb.In(node.PortIn))

payload := types.NewString(faker.Word())
payload, err := port.Call(in, payload)
```
41 changes: 5 additions & 36 deletions docs/user_extensions_kr.md
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,10 @@ defer r.Close()

이제 만든 런타임 환경을 기존 서비스에 통합하고, 다시 빌드하여 실행 파일을 생성해야 합니다.

### 지속 실행

런타임 환경을 지속적으로 유지하면 외부 요청에 즉시 대응할 수 있습니다. 각 런타임 환경은 독립적인 컨테이너에서 실행되며, 지속적인 워크플로우 실행이 필요한 시나리오에 적합합니다.

```go
func main() {
ctx := context.TODO()

specStore := spec.NewStore()
secretStore := secret.NewStore()

Expand Down Expand Up @@ -372,39 +370,10 @@ func main() {
_ = r.Close()
}()

r.Listen(context.TODO())
r.Watch(ctx)
r.Load(ctx)
r.Reconcile(ctx)
}
```

위 코드에서는 런타임 환경을 지속적으로 실행하여 외부 신호에 반응하도록 설정합니다. `os.Signal`을 통해 종료 신호를 수신하면 런타임 환경을 안전하게 종료합니다.

### 단순 실행

때로는 런타임 환경을 지속적으로 유지하는 대신, 필요할 때만 실행하고 종료하는 간단한 방식이 더 적합할 수 있습니다. 이럴 때는 단순 실행 방식을 사용할 수 있습니다.

```go
r := runtime.New(runtime.Config{
Namespace: "default",
Schema: scheme,
Hook: hook,
SpecStore: specStore,
SecretStore: secretStore,
})
defer r.Close()

r.Load(ctx) // 모든 리소스 로드

symbols, _ := r.Load(ctx, &spec.Meta{
Name: "main",
})

sb := symbols[0]

in := port.NewOut()
defer in.Close()

in.Link(sb.In(node.PortIn))

payload := types.NewString(faker.Word())
payload, err := port.Call(in, payload)
```
6 changes: 3 additions & 3 deletions driver/mongo/pkg/secret/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,18 @@ func (s *Store) Swap(ctx context.Context, secrets ...*secret.Secret) (int, error
}
defer cursor.Close(ctx)

exists := make(map[uuid.UUID]*secret.Secret)
ok := make(map[uuid.UUID]*secret.Secret)
for cursor.Next(ctx) {
sec := &secret.Secret{}
if err := cursor.Decode(&sec); err != nil {
return 0, err
}
exists[sec.GetID()] = sec
ok[sec.GetID()] = sec
}

count := 0
for _, sec := range secrets {
exist, ok := exists[sec.GetID()]
exist, ok := ok[sec.GetID()]
if !ok {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions driver/mongo/pkg/spec/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,18 @@ func (s *Store) Swap(ctx context.Context, specs ...spec.Spec) (int, error) {
}
defer cursor.Close(ctx)

exists := make(map[uuid.UUID]spec.Spec)
ok := make(map[uuid.UUID]spec.Spec)
for cursor.Next(ctx) {
sp := &spec.Unstructured{}
if err := cursor.Decode(sp); err != nil {
return 0, err
}
exists[sp.GetID()] = sp
ok[sp.GetID()] = sp
}

count := 0
for _, sp := range specs {
exist, ok := exists[sp.GetID()]
exist, ok := ok[sp.GetID()]
if !ok {
continue
}
Expand Down
7 changes: 2 additions & 5 deletions ext/pkg/control/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ func TestAddToScheme(t *testing.T) {

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, ok := s.KnownType(tt)
assert.True(t, ok)

_, ok = s.Codec(tt)
assert.True(t, ok)
assert.NotNil(t, s.KnownType(tt))
assert.NotNil(t, s.Codec(tt))
})
}
}
7 changes: 2 additions & 5 deletions ext/pkg/io/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ func TestAddToScheme(t *testing.T) {

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, ok := s.KnownType(tt)
assert.True(t, ok)

_, ok = s.Codec(tt)
assert.True(t, ok)
assert.NotNil(t, s.KnownType(tt))
assert.NotNil(t, s.Codec(tt))
})
}
}
7 changes: 2 additions & 5 deletions ext/pkg/network/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,8 @@ func TestAddToScheme(t *testing.T) {

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, ok := s.KnownType(tt)
assert.True(t, ok)

_, ok = s.Codec(tt)
assert.True(t, ok)
assert.NotNil(t, s.KnownType(tt))
assert.NotNil(t, s.Codec(tt))
})
}
}
7 changes: 2 additions & 5 deletions ext/pkg/system/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ func TestAddToScheme(t *testing.T) {

for _, tt := range tests {
t.Run(tt, func(t *testing.T) {
_, ok := s.KnownType(tt)
assert.True(t, ok)

_, ok = s.Codec(tt)
assert.True(t, ok)
assert.NotNil(t, s.KnownType(tt))
assert.NotNil(t, s.Codec(tt))
})
}
}
12 changes: 6 additions & 6 deletions ext/pkg/system/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func UpdateNodes(s spec.Store) func(context.Context, []spec.Spec) ([]spec.Spec,

func DeleteNodes(s spec.Store) func(context.Context, []spec.Spec) ([]spec.Spec, error) {
return func(ctx context.Context, specs []spec.Spec) ([]spec.Spec, error) {
exists, err := s.Load(ctx, specs...)
ok, err := s.Load(ctx, specs...)
if err != nil {
return nil, err
}
if _, err := s.Delete(ctx, exists...); err != nil {
if _, err := s.Delete(ctx, ok...); err != nil {
return nil, err
}
return exists, nil
return ok, nil
}
}

Expand Down Expand Up @@ -84,13 +84,13 @@ func UpdateSecrets(s secret.Store) func(context.Context, []*secret.Secret) ([]*s

func DeleteSecrets(s secret.Store) func(context.Context, []*secret.Secret) ([]*secret.Secret, error) {
return func(ctx context.Context, secrets []*secret.Secret) ([]*secret.Secret, error) {
exists, err := s.Load(ctx, secrets...)
ok, err := s.Load(ctx, secrets...)
if err != nil {
return nil, err
}
if _, err := s.Delete(ctx, exists...); err != nil {
if _, err := s.Delete(ctx, ok...); err != nil {
return nil, err
}
return exists, nil
return ok, nil
}
}
Loading

0 comments on commit d24989a

Please sign in to comment.